public class UploadHandler : IhttpHandler{ public voID ProcessRequest(httpContext context) { context.Response.ContentType = "text/plain"; try { string dirFullPath = httpContext.Current.Server.MapPath("~/Uploader/"); string[] files; int numfiles; files = System.IO.Directory.Getfiles(dirFullPath); numfiles = files.Length; numfiles = numfiles + 1; string str_image = ""; foreach (string s in context.Request.files) { httpPostedfile file = context.Request.files[s]; string filename = file.filename; string fileExtension = file.ContentType; if (!string.IsNullOrEmpty(filename)) { fileExtension = Path.GetExtension(filename); str_image = "MyPHOTO_" + numfiles.ToString() + fileExtension; string pathToSave_100 = httpContext.Current.Server.MapPath("~/Uploader/") + str_image; file.SaveAs(pathToSave_100); } } // database record update logic here () context.Response.Write(str_image); } catch (Exception ac) { } } public bool IsReusable { get { return false; } }}
JsCode
/Image Upload codefunction sendfile(file) { var formData = new FormData(); formData.append('file',$('#f_UploadImage')[0].files[0]); $.AJAX({ url: 'UploadHandler.ashx',type: 'POST',data: formData,cache: false,processData: false,ContentType: false,success: function(result) { if (result != 'error') { var my_path = "Uploader/" + result; $("#myUploadedimg").attr("src",my_path); } },error: function(err) { alert(err.statusText); } });}function callimgUploader() { var _URL = window.URL || window.webkitURL; $("#f_UploadImage").on('change',function() { var file,img; if ((file = this.files[0])) { img = new Image(); img.onload = function() { sendfile(file); }; img.onerror = function() { alert("Not a valID file:" + file.type); }; img.src = _URL.createObjectURL(file); } });}
注意:我的Aspx页面是不同的文件夹和Image Folder和UploadHandler.ashx.cs是路径文件夹错了吗?
运行AJAX请求后每次给出Not-Found错误怎么能修复它.
谢谢.
解决方法 您没有提到您正在使用哪个上传控件,我假设它是服务器端,您需要按如下方式访问它更改
$('#f_UploadImage')
至
$('#<%= f_UploadImage.ClIEntID %>')总结
以上是内存溢出为你收集整理的c# – 使用jquery和handler(ashx)在上传文件中找不到’错误’全部内容,希望文章能够帮你解决c# – 使用jquery和handler(ashx)在上传文件中找不到’错误’所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)