1 <div class="col-xs-12 mcp-list-item" style="margin-top:20px"> 2 <div class="mcp-list-item-title">附件</div> 3 <input onchange="upload()" id="uploadFile" name="uploadFile" placeholder="多個請按下Ctrl后選取文件" type="file" value="文件選取" multiple="multiple"> 4 </div> 5 <div class="col-xs-12 mcp-list-item"> 6 <a class="btn btn-success" onclick="btn_upload()"><i class="fa fa-upload"></i>開始上傳</a> 7 </div>
//下面這一段代碼是用來顯示圖片容器的
<div id="imgList"> <img id="fileUrl" style="width: 100px; height: 100px; border-radius: 100px;" /> </div> <div id="fileList"> </div>
//圖片上傳 function upload() { var f1 = document.getElementById('uploadFile').files; if (f1.length>0) { var form = $('#imgList'); for (var i = 0; i < f1.length; i++) { var src1 = window.URL.createObjectURL(f1[i]); var imgid='fileUrl_'+i; var oldElement = $('#fileUrl'); var newElement = $(oldElement).clone(); $(oldElement).attr('id', imgid); $(oldElement).before(newElement); $(oldElement).appendTo(form); $(oldElement).attr('src', src1); // $('#' + imgid).src = src1 } } //var f = document.getElementById('uploadFile').files[0]; //var src = window.URL.createObjectURL(f); //var filetype = "GIF,JPEG,JPG,PNG,pdf"; //document.getElementById('fileUrl').src = src; //if (f == null || f == "") { // dialogMsg('請選擇文件!', 0); // return; //} else if (f != "" && f != undefined && f != null) { // var imgtype = ff.toLowerCase().split('.'); // if (filetype.indexOf(imgtype[1].toUpperCase()) == -1) { // dialogMsg('圖片類型必須是.gif,jpeg,jpg,png,pdf中的一種!', 0); // return; // } //} } function btn_upload() { var refids = []; var rows = $('#Delivery_Grid').datagrid('getRows'); $.each(rows, function (index, item) { refids.push(item.reforderid); }); var numArr = []; var txt = $("#fileList").find("input:file"); //獲取所有上傳附件框 for (var i = 0; i < txt.length; i++) { numArr.push(txt.eq(i).attr('id')); //將附件框的ID添加到數組中 } if (!$('#formState').Validform()) { return false; } var postData = $("#formState").GetWebControls(); $.ajaxFileUpload({ url: "/TMS.Service/ServiceMilestone/UploadFile?reforderid=" + refids.join(','), secureuri: false, fileElementId: 'uploadFile', dataType: 'json', //data: postData, success: function (data) { if (data.status) { dialogMsg(data.message, 1); document.getElementById('fileUrl').src = ""; document.getElementById('uploadFile').src = ""; } else { dialogMsg(data.msg, 0); } } }); }
記得引入 <script src="~/Content/scripts/uploadify/ajaxfileupload.js"></script>
后台代碼:
[HttpPost] public ActionResult UploadFile(MilestoneInfo minfo) { try { var filelist = new List<Cargo.Entity.FileInfoEntity>(); if (minfo == null) { throw new Exception("找不到milestone節點所屬的業務信息 "); } if (Request != null && Request.Files != null && Request.Files.Count > 0) { string FileServerUrl = ConfigurationManager.AppSettings["FileServerUrl"].ToString(); string serverBasePath = AppDomain.CurrentDomain.BaseDirectory + @"Upload\temp\"; DirectoryInfo dir = new DirectoryInfo(serverBasePath); if (!dir.Exists) { dir.Create(); } foreach (string fileStr in Request.Files) { HttpPostedFileBase file = Request.Files[fileStr] as HttpPostedFileBase; if (file != null && file.ContentLength > 0) { string filename = Path.GetFileName(file.FileName); string savePath = serverBasePath + @"\" + filename; var fileExtName = Path.GetExtension(savePath); file.SaveAs(savePath); string uploadDate = DateTime.Now.ToString("yyyyMMdd"); string address = FileServerUrl + $"Home/UploadFile?module={TMS_Enum.TMS_SysFileInfoEnum.TMS_module.ToString()}&folderId={TMS_Enum.TMS_SysFileInfoEnum.TMS_folderId.ToString()}&uploadDate=" + uploadDate; var buffer = new WebClient().UploadFile(address, "post", savePath); string result = Encoding.UTF8.GetString(buffer); var serverFilePath = FileServerUrl + result.Replace("~/", ""); //最終服務器存儲的地址 //判斷文件是不是存在 清空臨時存儲的文件 if (System.IO.File.Exists(savePath)) { //如果存在則刪除 System.IO.File.Delete(savePath); } filelist.Add(new Cargo.Entity.FileInfoEntity { FileExtensions = fileExtName, FilePath = serverFilePath, FileName = filename, }); } } } //這里為了避免上傳文件事務長時間鎖死,所以放置到這里進行操作,先上傳耗時的文件操作,再考慮操作數據庫 SaveMilestone( minfo, filelist); return new ResponseModel { status = true, msg = "提交成功!" }; } catch (Exception ex) { return new ResponseModel { status = false, msg = ex.Message }; } }



