我正在尝试使用带有处理程序(c#)的
jquery ajax上传文件.问题是,当我调用处理程序时,我得到了
context.Request.File.Count=0
这是aspx代码:
<!--aspx file code--> <script language="javascript" type="text/javascript"> $().ready(function () { $('#save').click(function (e) { CalluploaderHandler(); }); }); function CalluploaderHandler() { $.ajax({ type: "POST",url: "Services/UPloader.ashx",contentType: "application/json; charset=utf-8",success: OnComplete,error: OnFail }); return false; } function OnComplete(result) { alert('Success'); } function OnFail(result) { alert('Request Failed'); } </script> </head> <body> <form enctype="multipart/form-data"> <label for="file"> Filename:</label> <input name="file" id="file" type="file"> <input id="save" name="submit" value="Submit" type="submit"> </form> </body> </html>
c#代码处理程序:
/* handler*/ public void ProcessRequest(HttpContext context) { string savedFileName = ""; foreach (string file in context.Request.Files) { HttpPostedFile hpf = context.Request.Files[file] as HttpPostedFile; if (hpf.ContentLength == 0) continue; // savedFileName = context.Server.MapPath(Path.GetFileName(hpf.FileName)); // hpf.SaveAs(savedFileName); } context.Response.Write(savedFileName); }
解决方法
我认为问题在于使用contentType试试
contentType: 'multipart/form-data',
要么
contentType :'application/octet-stream';
有关更多信息,请参阅此帖子