国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

asp.net多文件上傳實(shí)例講解

2024-07-10 13:31:29
字體:
供稿:網(wǎng)友

文件上傳簡(jiǎn)單實(shí)現(xiàn)是非常容易的,但是想要更高的要求,比如通過ajax上傳文件、一次上傳多個(gè)文件、文件比較大等等,這里面的坑就不是很容易填(對(duì)于新手來說)。因此在這里我準(zhǔn)備通過ajax實(shí)現(xiàn)多文件上傳。在開始貼代碼之前,要注意幾點(diǎn):

  1.<input type="file" />是必須要加name的,不知道為什么不加name屬性,后臺(tái)獲取不到文件數(shù)據(jù)(有辦法的大神可以在評(píng)論區(qū)提醒我),然后是multiple屬性,當(dāng)multiple="multiple"時(shí),file控件是可以多選需要上傳的文件的(<input type="file" multiple="multiple"  name="myFile" />)。

  2.form要設(shè)enctype為multiple/form-data,multipart/form-data表示:不對(duì)字符編碼,在使用包含文件上傳控件的表單時(shí),必須使用該值。

  3.重點(diǎn)來了,ajax的參數(shù)設(shè)置里面有大坑(很多人都沒注意ajax的眾多參數(shù)),contentType和processData需要設(shè)為false,contentType明明被要求為string類型,但是這里要設(shè)為false(我也不知道為什么),網(wǎng)上關(guān)于contentType的說明大多是"contentType:要求為String類型的參數(shù),當(dāng)發(fā)送信息至服務(wù)器時(shí),內(nèi)容編碼類型默認(rèn)為"application/x-www-form-urlencoded"。該默認(rèn)值適合大多數(shù)應(yīng)用場(chǎng)合",還有個(gè)data要設(shè)為new FormData($(' ')[0])。

  下面就是簡(jiǎn)單的前臺(tái)代碼:

<form id="uploadForm" enctype="multipart/form-data" action="/Login/uploadFile" method="post"> <input type="file" multiple="multiple" id="PersonFile" name="MyFile" /> <button type="button" id="submitFile" onclick="uploadFile()">提交</button></form>
//上傳文件 function uploadFile() {  debugger  $.ajax({  url: '/Login/uploadFile',  type: 'POST',  cache: false,  data: new FormData($('#uploadForm')[0]),  processData: false, // 關(guān)鍵點(diǎn)  contentType: false, // 關(guān)鍵點(diǎn)  success: function (result) {   if (result.Check) {   alert("成功");   }   else {   alert("失敗");   }   var file = $("#PersonFile")   file.after(file.clone().val(""));   file.remove();  }  }); }

現(xiàn)在輪到后臺(tái)了,我這邊后臺(tái)是通過System.Web.HttpContext.Current.Request.Files獲取文件數(shù)據(jù)集的,之后的代碼我將以圖片為例。

 [HttpPost] public ActionResult uploadFile() {  Result<string> check = new Result<string>();  try  {  HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;  int number = 0;  for (int i = 0; i < files.Count; i++)  {   System.Text.StringBuilder fileName = new System.Text.StringBuilder();   fileName.Append(@"D:/");   fileName.Append(DateTime.Now.ToString("yyMMdd"));   fileName.Append(@"/");   if (!System.IO.Directory.Exists(fileName.ToString()))   {   System.IO.Directory.CreateDirectory(fileName.ToString());   }   fileName.Append(System.IO.Path.GetFileNameWithoutExtension(files[i].FileName));   fileName.Append(DateTime.Now.ToString("yyMMddHHmmss"));   fileName.Append(System.IO.Path.GetExtension(files[i].FileName));   System.IO.Stream sm = files[i].InputStream;   if (System.IO.File.Exists(@"D:/水印log.jpg"))   {   ImageHelper.ZoomAuto(sm, fileName.ToString(), 400, 300, "", @"D:/水印log.jpg");   }   else   {   ImageHelper.ZoomAuto(sm, fileName.ToString(), 400, 300, "水印LOG", "");   }   bool ok = System.IO.File.Exists(fileName.ToString());   if (ok)   {   number++;   }  }  if (number.Equals(files.Count))  {   check.Message = "上傳成功!";   check.Check = true;  }  else  {   check.Message = "失敗!";   check.Check = false;  }  return Json(check);  }  catch(Exception ex)  {  check.Message = ex.ToString();  check.Check = false;  return Json(check);  } }
 /// <summary> /// 返回值 /// </summary> public class Result<T> { public string Message { get; set; } public bool Check { get; set; } public IList<T> ResultList { get; set; } }

  其中用到了ImageHelper.ZoomAuto(),這個(gè)是吳劍大哥寫的圖片處理類,地址http://www.cnblogs.com/wu-jian/archive/2011/02/21/1959382.html。最后還有一個(gè)坑,就是asp.net對(duì)一次上傳數(shù)據(jù)的大小是有限制的,要解除限制才能10個(gè)20個(gè)文件同時(shí)上傳。如何解除限制?在web.config里面配置一下就OK了。代碼如下:

<system.web> <authentication mode="None" /> <compilation debug="true" targetFramework="4.5" /> <!--<httpRuntime targetFramework="4.5" />--> <httpRuntime executionTimeout="500" maxRequestLength="409600" useFullyQualifiedRedirectUrl="false" minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="100" /> </system.web>

把<httpRuntime >放<system.web>節(jié)點(diǎn)下。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到ASP.NET教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 张北县| 安阳县| 黑山县| 彭州市| 中西区| 江达县| 山丹县| 勐海县| 石楼县| 烟台市| 蓬溪县| 措勤县| 张掖市| 鱼台县| 老河口市| 湖南省| 灵川县| 永修县| 黑山县| 虹口区| 屯留县| 三穗县| 鱼台县| 紫阳县| 永新县| 美姑县| 泾川县| 迭部县| 南投市| 叶城县| 无极县| 延长县| 金沙县| 南雄市| 资源县| 遵义县| 东丽区| 阜南县| 安乡县| 九龙县| 墨脱县|