日期:2014-05-18  浏览次数:20402 次

怎么获取不到上传的文件???``````````
JS部分

function   submit_order()  
{  
document.getElementById( "userfile ").click();
window.confirm(document.getElementById( "userfile ").value)  
window.location.href= " <%=Save()%> ";  
}


CS部分

private   void   Page_Load(object   sender,   System.EventArgs   e)
{
Button1.Attributes.Add( "onclick ", "submit_order(); ");
}
public   string   Save()
{
HttpFileCollection   file   =HttpContext.Current.Request.Files;
for(int   n   =   0;n   <file.Count;n++)
{
HttpPostedFile   postfile   =   file[n];
string   fileName   =   Path.GetFileName(postfile.FileName);
string   savePath   =   System.Web.HttpContext.Current.Request.MapPath( "d:/ ")+fileName;
postfile.SaveAs(savePath);
}
return   "heihei.aspx ";
}


麻烦大家看看```
我获取的文件数总是为0

------解决方案--------------------
你的文件总数从哪里获得呀??没看出来!!
------解决方案--------------------
js 不可以那么调用save()吧?
------解决方案--------------------
js 不可以那么调用save()吧?
--------------------------------------
当前可以!!
------解决方案--------------------
<form id= "Form1 " method= "post " runat= "server ">
这个改成
<form id= "form1 " enctype= "multipart/form-data " method= "post " runat= "server " >

------解决方案--------------------
嗯,还有一个问题
<input id= "userfile " type= "file " runat= "server " style= "display:none ">
需要加上name属性,否则不会提交的
<input id= "userfile " type= "file " name= "aa " runat= "server " style= "display:none ">


------解决方案--------------------
哦,对了
document.getElementById( "userfile ").click();
这种方法虽然可以选择文件却无法提交,必须要可见的file对象。 这是上载文件控件的一大怨念啊。
------解决方案--------------------
http://dotnet.aspx.cc/ShowDetail.aspx?id=58EA3515-36F2-4FD9-AC89-EAF49F59816C
------解决方案--------------------
<input id= "userfile " type= "file " name= "aa " runat= "server " style= "display:none ">
改成
<input id= "userfile " type= "file " name= "aa " style= "display:none ">
runat= "server " 去掉就好了
------解决方案--------------------
<form id= "form1 "里加了 enctype= "multipart/form-data "就不要加runat= "server " !用下面的代码试试看
HttpFileCollection filea=Request.Files;
HttpPostedFile pFile=filea[0];
int fLenth=pFile.ContentLength;
------解决方案--------------------
<form id= "form1 "里加了 enctype= "multipart/form-data ", <input id= "userfile " type= "file "就不要加runat= "server " !用下面的代码试试看
HttpFileCollection filea=Request.Files;
HttpPostedFile pFile=filea[0];
int fLenth=pFile.ContentLength;