使用表单文件域(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);