==========文件上传问题,配合ajax,我的方法怎么不行!
1,
WorkNoteService.asmx文件部分代码:
<%@ WebService Language= "C# " Class= "WorkNoteService " %>
......
public partial class WorkNoteService : System.Web.Services.WebService
{
......
[WebMethod]
public void UploadFile()
{
DateTime nowTime=DateTime.Now;
string savePath=Server.MapPath( "\\Attachments\\ ");
string filename= " ";
HttpFileCollection files = HttpContext.Current.Request.Files;
for(int i=0;i <files.Count;i++)
{
HttpPostedFile postedFile=files[i];
filename=nowTime.ToString( "yyyymmddhhss ")+System.IO.Path.GetFileName(postedFile.FileName);
postedFile.SaveAs(savePath+filename);
}
}
}
2,diarynew.aspx
<script language= "javascript " type= "text/javascript ">
<!--
function Button_onclick()
{
WorkNoteService.UploadFile();
}
// -->
</script>
......
<form runat= "server " enctype= "multipart/form-data ">
<asp:ScriptManager ID= "ScriptManager1 " runat= "server ">
<Services>
<asp:ServiceReference Path= "WorkNoteService.asmx " />
</Services>
</asp:ScriptManager>
......
<input type= "file " runat= "server " id= "file1 " size= "40 "/>
<input id= "Button1 " style= "width: 80px " type= "button " value= "添加 " language= "javascript " onclick= "Button_onclick() " />
</form>
上面點擊後,文件總是上傳不了。哪裡不對呢!?
谢谢!
------解决方案--------------------一切跟与服务器通信的类,或者控件,在AJAX方法里都不能使用。 你自己找找原因吧!
------解决方案--------------------例如Request, 你可以用一个全局变量保存接受到的值,在AJAX方法里使用全局变量。AJAX方法带参数,从前台传值
------解决方案--------------------找出你AJAX方法中与服务器有联系的地方, 把有联系的地方都写成参数从前台传过来