jquery - ASP.Net C# website functionality not working on iis -
i have website built in asp.net c# , jquery functionality. find functionality (eg delete files) not work unless step through code in debug mode. none of functionality works on site on localhost (iis)
i using file handler (filetransferhandler.ashx)to delete, upload files. said above works when step through code in debug mode not when run on localhost(iis)
the error seem getting is: 405 page looking cannot displayed because invalid method (http verb) being used
here code
using system.collections.generic; using system.configuration; using system.io; using system.linq; using system.web; using system.web.script.serialization; using system.web.sessionstate; public class filetransferhandler : ihttphandler, ireadonlysessionstate { private readonly javascriptserializer js = new javascriptserializer(); public string storageroot { { return configurationmanager.appsettings["storageroot"] ; } } public bool isreusable { { return false; } } public void processrequest(httpcontext context) { context.response.addheader("pragma", "no-cache"); context.response.addheader("cache-control", "private, no-cache"); handlemethod(context); } // handle request based on method private void handlemethod(httpcontext context) { switch (context.request.httpmethod) { case "head": case "get": if (givenfilename(context)) deliverfile(context); else listcurrentfiles(context); break; case "post": case "put": uploadfile(context); break; case "delete": deletefile(context); break; case "options": returnoptions(context); break; default: context.response.clearheaders(); context.response.statuscode = 405; break; } } private static void returnoptions(httpcontext context) { context.response.addheader("allow", "delete,get,head,post,put,options"); context.response.statuscode = 200; } // delete file server private void deletefile(httpcontext context) { var filepath = storageroot + context.session["uname"].tostring() + "/" + context.request["f"]; if (file.exists(filepath)) { file.delete(filepath); } } // upload file server private void uploadfile(httpcontext context) { var statuses = new list<filesstatus>(); var headers = context.request.headers; if (string.isnullorempty(headers["x-file-name"])) { uploadwholefile(context, statuses); } else { uploadpartialfile(headers["x-file-name"], context, statuses); } writejsoniframesafe(context, statuses); } // upload partial file private void uploadpartialfile(string filename, httpcontext context, list<filesstatus> statuses) { if (context.request.files.count != 1) throw new httprequestvalidationexception("attempt upload chunked file containing more 1 fragment per request"); var inputstream = context.request.files[0].inputstream; var fullname = storageroot+ context.session["uname"].tostring() + "/" + path.getfilename(filename); using (var fs = new filestream(fullname, filemode.append, fileaccess.write)) { var buffer = new byte[1024]; var l = inputstream.read(buffer, 0, 1024); while (l > 0) { fs.write(buffer, 0, l); l = inputstream.read(buffer, 0, 1024); } fs.flush(); fs.close(); } statuses.add(new filesstatus(new fileinfo(fullname))); } // upload entire file private void uploadwholefile(httpcontext context, list<filesstatus> statuses) { (int = 0; < context.request.files.count; i++) { var file = context.request.files[i]; file.saveas(storageroot + context.session["uname"].tostring()+ "/" + path.getfilename(file.filename)); string fullname = path.getfilename(file.filename); statuses.add(new filesstatus(fullname, file.contentlength)); } } private void writejsoniframesafe(httpcontext context, list<filesstatus> statuses) { context.response.addheader("vary", "accept"); try { if (context.request["http_accept"].contains("application/json")) context.response.contenttype = "application/json"; else context.response.contenttype = "text/plain"; } catch { context.response.contenttype = "text/plain"; } var jsonobj = js.serialize(statuses.toarray()); context.response.write(jsonobj); } private static bool givenfilename(httpcontext context) { return !string.isnullorempty(context.request["f"]); } private void deliverfile(httpcontext context) { var filename = context.request["f"]; var filepath = storageroot + context.session["uname"].tostring() + "/"+ filename; if (file.exists(filepath)) { context.response.addheader("content-disposition", "attachment; filename=\"" + filename + "\""); context.response.contenttype = "application/octet-stream"; context.response.clearcontent(); context.response.writefile(filepath); } else context.response.statuscode = 404; } private void listcurrentfiles(httpcontext context) { var files = new directoryinfo(storageroot + "/" + context.session["uname"].tostring()) .getfiles("*", searchoption.topdirectoryonly) .where(f => !f.attributes.hasflag(fileattributes.hidden)) .select(f => new filesstatus(f)) .toarray(); string jsonobj = js.serialize(files); context.response.addheader("content-disposition", "inline; filename=\"files.json\""); context.response.write(jsonobj); context.response.contenttype = "application/json"; } }
even had faced issue after clearing cookie , cache of browser, has been resolved.
Comments
Post a Comment