1. 程式人生 > >.net 多文件上傳

.net 多文件上傳

use tde com get pub 獲取文件 def eas style

版權聲明:本文為博主原創文章,未經博主允許不得轉載。

1.頁面

<head runat="server">
    <title>上傳文件</title>
    <!--#include file="/_Layouts/include/Common.htm" -->
    <link href="/_Layouts/res/css/ahtf.css" rel="stylesheet" type="text/css" />
    <script src="../res/js/calendarCN.js" type="text/javascript"></script>
    <style type="
text/css"> .style1 { width: 50px; } </style> <script type="text/javascript"> var appId, appType, aDefId; $(function () { appType = GetQueryString("appType"); aDefId = GetQueryString("
activityId"); appId = GetQueryString("appId"); }); //獲取請求參數 function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) return
decodeURI(r[2]); return null; } //新增一行 function insertRows() { var tb = $("#tbUpload"); //找到table的ID var rowNum = $("#tbUpload tr").size(); //獲取表格的行數 var tempRow = rowNum - 1; ; //用於新增tr、td、屬性下標 var $tdEm = $("<td></td>"); //創建一個td var $tdFile = $("<td></td>"); //創建一個td $tdEm.html(""); //空的 $tdFile.html("<input type =\‘file\‘ id=\‘UploadAttachment" + (tempRow + 1) + "\‘ name=\‘UploadAttachment" + (tempRow + 1) + "\‘ value=\‘\‘ style=\‘width: 450px;\‘/>" + "&nbsp;&nbsp; <a href=\‘#\‘ name=\‘teams[" + tempRow + "].del\‘ onclick=\‘delRows(" + tempRow + ")\‘>刪除</a>"); //文件上傳控件和刪除按鈕 var $tr = $("<tr id=\‘tr_" + tempRow + "\‘ ></tr>"); // 創建tr,將td放置到tr中 $tr.append($tdEm); $tr.append($tdFile); //在表格的最後追加新增的tr tb.append($tr); } //刪除某行 function delRows(sub) { var rowNum = sub + 1; //行號=下標+1 var tb = $("#tbUpload"); //找到table的ID var tempRow = $("#tbUpload tr").size(); //獲取表格的行數 if (tempRow > rowNum) { $("#tr_" + sub).remove(); //刪除tr for (i = (parseInt(sub) + 1); i < tempRow - 1; i++) { //把刪除的行後面的列及元素都向前挪一位 $("#tr_" + i).attr("id", "tr_" + (i - 1) + ""); $("input[name=‘UploadAttachment" + (i + 1) + "‘]").attr("name", "UploadAttachment" + i); $("a[name=‘teams[" + i + "].del").attr("onclick", "delRows(" + (i - 1) + ")"); $("a[name=‘teams[" + i + "].del").attr("name", "teams[" + (i - 1) + "].del"); } } } //保存上傳的文件 function saveAttachment() { var count = $("#tbUpload tr").size(); $.ajaxFileUpload ( { url: /_Layouts/wf/Controller/JyzdcgtzgtzdWfManager.ashx?method=SaveAttachment, //用於文件上傳的服務器端請求地址 type: POST, data: { appId: appId, aDefId: aDefId, appType: appType, count: count }, secureuri: false, //一般設置為false fileElementId: [UploadAttachment0], //‘UploadAttachment1‘, ‘UploadAttachment2‘, ‘UploadAttachment3‘, ‘UploadAttachment4‘, ‘UploadAttachment5‘, ‘UploadAttachment6‘], //文件上傳空間的id屬性 dataType: JSON, //返回值類型 一般設置為json success: function (data) //服務器成功響應處理函數 { data = $.parseJSON(data); if (data.success) { window.opener.SearchAttachment(appId, aDefId, "0,1", data.fileId); $.messager.alert(提示, data.message, info); window.close(); } else { $.messager.alert(提示, data.message, error); } } } ); } </script> </head> <body> <form id="form1" runat="server"> <input id="AppId" type="hidden" value="" /> <div> <table id="tbUpload" border="0" style="width: 800px; text-align: left; font-size: 12px;"> <tr id="trFirst" style="height: 35px"> <td class="style1"> 上傳文件 </td> <td> <input type="file" name="UploadAttachment0" id="UploadAttachment0" style="width: 450px;" />&nbsp;&nbsp; <a href="#" onclick="insertRows();">添加</a> &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold"> <a id="UploadAttachment_btnSubmit" href="javaScript:void(0)" onclick="saveAttachment()"> 上傳</a></span> <%--<a id="Span1" href="#UploadAttachment_btnSubmit" style="color: red; ">請選擇要上傳的文件</a> 關於錨鏈接 的實驗 感覺跟label for差不多--%> <span id="UploadAttachment_RequiredFieldValidator1" style="color: red; display: none;"> 請選擇要上傳的文件</span> </td> </tr> </table> <%--上傳文件:<input type="file" name="UploadAttachment1$FileUpload" id="UploadAttachment1_FileUpload" style="width:450px;"> 添加 上傳--%> </div> </form> </body> </html>

後臺SaveAttachment方法:

#region 保存上傳的文件 modify by wh 2017022
        //[WNet("WNet1")]
        public void SaveAttachment(HttpContext context)
        {
            try
            {
                #region 參數
                string appId = context.Request["appId"];
                string aDefId = context.Request["aDefId"];
                string appType = context.Request["appType"];
                int count = int.Parse(context.Request["count"]);
                #endregion

                string fileId = string.Empty;
                int flag = 0;

                //context.Request.Files.AllKeys

                //接收上傳後的文件
                for (int i = 0; i < count; i++)
                {
                    HttpPostedFile file = context.Request.Files["UploadAttachment" + i + ""];
                    //獲取文件的文件名(不包括文件的路徑)
                    if (file == null)
                    {
                        continue;
                    }
                    string fileName = file.FileName;
                    if (!string.IsNullOrEmpty(fileName))
                    {
                        string path = AppHelper.GetSysPath(SysFolder.Lgfz);
                        if (appType == "JYZDCGTZG")
                        {
                            path += "/Jyzdcgt/";
                        }
                        else if (appType == "JYZYCSXSB")
                        {
                            path += "/Jyzycsx/";
                        }
                        else if (appType == "YKDCGTZG")
                        {
                            path += "/Ykdcgt/";
                        }
                        // 判斷路徑是否存在,不存在則新建路徑
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }

                        FileModel fileInf = new FileModel();
                        string guid = Guid.NewGuid().ToString();
                        string filePath = path + guid + "_" + Path.GetFileName(fileName);
                        //保存文件
                        file.SaveAs(filePath);

                        fileInf.FileId = guid;
                        fileInf.FileName = Path.GetFileName(file.FileName);//獲取文件的文件名(不包括文件的路徑)
                        fileInf.FileAddress = path + guid + "_" + fileName;//文件存放地址
                        fileInf.ActivityId = aDefId;
                        fileInf.AppId = appId;
                        fileInf.CreateUser =  CurrentUser.User.UserAlias;
                        fileInf.LastModifyUser = CurrentUser.User.UserAlias;
                        fileInf.Status = "0";//status為0——新增未保存

                        // 保存文件信息到數據庫
                        Dao.SaveAttachment(fileInf);
                        flag++; 
                        fileId += fileInf.FileId;
                        if (i != count - 1) {
                            fileId += ",";
                        }
                    }
                }
                if (flag > 0)
                {
                    Dictionary<string, object> map = new Dictionary<string, object>();
                    map["success"] = true;
                    map["message"] = "文件上傳成功!";
                    map["fileId"] = fileId;// fileInf.FileId;
                    context.Response.Write(JsonHelper.getBeanJson(map));
                }
                else
                {
                    Dictionary<string, object> map = new Dictionary<string, object>();
                    map["success"] = false;
                    map["message"] = "文件為空!";
                    context.Response.Write(JsonHelper.getBeanJson(map));
                }
            }
            catch (Exception ex)
            {
                Dictionary<string, object> map = new Dictionary<string, object>();
                map["success"] = false;
                map["message"] = "文件上傳失敗!";
                context.Response.Write(JsonHelper.getBeanJson(map));
                Log.Error("保存文件信息異常:" + ex.Message);
            }
        }
        #endregion

應該就沒了

<head runat="server"> <title>上傳文件</title> <!--#include file="/_Layouts/include/Common.htm" --> <link href="/_Layouts/res/css/ahtf.css" rel="stylesheet" type="text/css" /> <script src="../res/js/calendarCN.js" type="text/javascript"></script> <style type="text/css"> .style1 { width: 50px; } </style> <script type="text/javascript"> var appId, appType, aDefId;
$(function () { appType = GetQueryString("appType"); aDefId = GetQueryString("activityId"); appId = GetQueryString("appId"); }); //獲取請求參數 function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) return decodeURI(r[2]); return null; }
//新增一行 function insertRows() { var tb = $("#tbUpload"); //找到table的ID var rowNum = $("#tbUpload tr").size(); //獲取表格的行數 var tempRow = rowNum - 1; ; //用於新增tr、td、屬性下標
var $tdEm = $("<td></td>"); //創建一個td var $tdFile = $("<td></td>"); //創建一個td $tdEm.html(""); //空的 $tdFile.html("<input type =\‘file\‘ id=\‘UploadAttachment" + (tempRow + 1) + "\‘ name=\‘UploadAttachment" + (tempRow + 1) + "\‘ value=\‘\‘ style=\‘width: 450px;\‘/>" + "&nbsp;&nbsp; <a href=\‘#\‘ name=\‘teams[" + tempRow + "].del\‘ onclick=\‘delRows(" + tempRow + ")\‘>刪除</a>"); //文件上傳控件和刪除按鈕 var $tr = $("<tr id=\‘tr_" + tempRow + "\‘ ></tr>"); // 創建tr,將td放置到tr中 $tr.append($tdEm); $tr.append($tdFile); //在表格的最後追加新增的tr tb.append($tr); }
//刪除某行 function delRows(sub) { var rowNum = sub + 1; //行號=下標+1 var tb = $("#tbUpload"); //找到table的ID var tempRow = $("#tbUpload tr").size(); //獲取表格的行數 if (tempRow > rowNum) { $("#tr_" + sub).remove(); //刪除tr for (i = (parseInt(sub) + 1); i < tempRow - 1; i++) { //把刪除的行後面的列及元素都向前挪一位 $("#tr_" + i).attr("id", "tr_" + (i - 1) + ""); $("input[name=‘UploadAttachment" + (i + 1) + "‘]").attr("name", "UploadAttachment" + i); $("a[name=‘teams[" + i + "].del").attr("onclick", "delRows(" + (i - 1) + ")"); $("a[name=‘teams[" + i + "].del").attr("name", "teams[" + (i - 1) + "].del"); } } }
//保存上傳的文件 function saveAttachment() { var count = $("#tbUpload tr").size(); $.ajaxFileUpload ( { url: ‘/_Layouts/wf/Controller/JyzdcgtzgtzdWfManager.ashx?method=SaveAttachment‘, //用於文件上傳的服務器端請求地址 type: ‘POST‘, data: { appId: appId, aDefId: aDefId, appType: appType, count: count }, secureuri: false, //一般設置為false fileElementId: [‘UploadAttachment0‘], //‘UploadAttachment1‘, ‘UploadAttachment2‘, ‘UploadAttachment3‘, ‘UploadAttachment4‘, ‘UploadAttachment5‘, ‘UploadAttachment6‘], //文件上傳空間的id屬性 dataType: ‘JSON‘, //返回值類型 一般設置為json success: function (data) //服務器成功響應處理函數 { data = $.parseJSON(data); if (data.success) { window.opener.SearchAttachment(appId, aDefId, "0,1", data.fileId); $.messager.alert(‘提示‘, data.message, ‘info‘); window.close(); } else { $.messager.alert(‘提示‘, data.message, ‘error‘); } } } ); }
</script></head><body> <form id="form1" runat="server"> <input id="AppId" type="hidden" value="" /> <div> <table id="tbUpload" border="0" style="width: 800px; text-align: left; font-size: 12px;"> <tr id="trFirst" style="height: 35px"> <td class="style1"> 上傳文件 </td> <td> <input type="file" name="UploadAttachment0" id="UploadAttachment0" style="width: 450px;" />&nbsp;&nbsp; <a href="#" onclick="insertRows();">添加</a> &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold"> <a id="UploadAttachment_btnSubmit" href="javaScript:void(0)" onclick="saveAttachment()"> 上傳</a></span> <%--<a id="Span1" href="#UploadAttachment_btnSubmit" style="color: red; ">請選擇要上傳的文件</a> 關於錨鏈接 的實驗 感覺跟label for差不多--%> <span id="UploadAttachment_RequiredFieldValidator1" style="color: red; display: none;"> 請選擇要上傳的文件</span> </td> </tr> </table> <%--上傳文件:<input type="file" name="UploadAttachment1$FileUpload" id="UploadAttachment1_FileUpload" style="width:450px;"> 添加 上傳--%> </div> </form></body></html>

.net 多文件上傳