html 上传控件

 使用表单文件域(input type=”file”)时,在PostBack中使用Request.Files获取不到文件。研究了半天,发现在input标签中使用runat=”server”后,是能够正常获取的。但是为了前端的元素ID不被修改,我尽可能不使用runat=”server”。

网上查阅了一下,最终让我找到了原因。用了高级货之后,忘本了呵呵。

要让form能够传递文件的话,必须要在form标签中加入enctype=”multipart/form-data”

<form enctype="multipart/form-data" ID="form1" runat="server">
      <input type="file" name="filename" />
</form>
 
  string strName = Request["name"].ToString();
                 HttpPostedFile hpf = this.fudImg.PostedFile;
                 if (id == 1)
                 {
                     if (hpf.ContentLength > 0)
                     {
                         string path = Server.MapPath(@"~\UpFile\Ads\");


                         string fileName = DateTime.Now.ToString("yyyyMMddhhMMss") + "-" + System.IO.Path.GetFileName(hpf.FileName);


                         string cPath = HtmlHelper.CreateDis(path);
                         //System.IO.Path.GetFileName(hpf.FileName)
                         path = path + cPath + @"\" + fileName;
                         string spath = @"UpFile/Ads/" + cPath + @"/" + fileName;
                         hpf.SaveAs(path);
string json = ""; string newfilename = ""; string path = ""; try { if (context.Request.Files["file_upload"] != null && context.Request.Files["file_upload"].FileName != "") { string hzm = System.IO.Path.GetExtension(context.Request.Files["file_upload"].FileName);//后缀名 如 .doc string[] a = { ".txt", ".jpg", ".jpeg", ".gif", ".png", ".docx", ".doc", ".xlsx", ".xls", ".rar", ".zip",".pdf" };//设定好了的格式 if (!a.Contains(hzm)) { json = "{\"statusCode\":\"300\",\"message\":\"文件格式不正确\",\"navTabId\":\"nav6\",\"rel\":\"\",\"callbackType\":\"\",\"forwardUrl\":\"\"}"; return json; } else { int defaulsize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["filesize"]);//取得设置的默认文件的大小 int filesize = (context.Request.Files["file_upload"].ContentLength) / 1024; //取得上传的文件的大小,单位为bytes if (filesize < defaulsize) { #region 对文件进行操作 newfilename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + hzm;//文件的新名字 如20120711105734222.doc path = System.Web.HttpContext.Current.Server.MapPath("~/UploadFile//");//文件保存的路径 if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } #endregion } else { //超过了文件的大小 json = "{\"statusCode\":\"300\",\"message\":\"上传的文件超过了3000M,请重新选择\",\"navTabId\":\"nav6\",\"rel\":\"\",\"callbackType\":\"\",\"forwardUrl\":\"\"}"; return json; } } } } catch (Exception) { json = "{\"statusCode\":\"300\",\"message\":\"文件格式不正确\",\"navTabId\":\"nav6\",\"rel\":\"\",\"callbackType\":\"\",\"forwardUrl\":\"\"}"; return json; } if (newfilename != "") { context.Request.Files["file_upload"].SaveAs(path + newfilename); //保存文件 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值