c# - Drag and Drop upload file by using Generic Handler in visual studio webform -
when trying pass files(drag , drop) generic handler, context.request.files.count in generic handler contains 0, i've tried lot of ways still unable solve it.
generic handler:
commonfunction _commonfunction = new commonfunction(); public void processrequest(httpcontext context) { context.response.contenttype = "text/plain"; httpfilecollection files = context.request.files; string dhidstr = context.request["dhid"]; int dhid = convert.toint32(dhidstr); //string destination = _commonfunction.generatefolderpath(dhid); string invalidfiles = ""; var appsettings = configurationmanager.appsettings; var dropboxfileext = appsettings["dropboxfileext"].tostring(); var dropboxfolder = appsettings["dropboxfolder"].tostring(); //var finaldestination = dropboxfolder.replace("[dealfolder]", destination); list<string> fileexts = dropboxfileext.split(',').tolist<string>(); if (context.request.files.count > 0) { foreach (string key in files) { httppostedfile file = files[key]; string filename = file.filename; string filetype = filename.substring(filename.lastindexof('.') + 1); if(fileexts.contains(filetype)) { string dir = system.io.path.combine(@dropboxfolder, filename); file.saveas(dir); } } } public bool isreusable { { return false; } }
site.js:
$(document).ready(function () { var box = document.getelementbyid("rmform"); box.addeventlistener("dragenter", ondragenter, false); box.addeventlistener("dragover", ondragover, false); box.addeventlistener("drop", ondrop, false); var upload = function (files) { var box = document.getelementbyid("box"); box.addeventlistener("dragenter", ondragenter, false); box.addeventlistener("dragover", ondragover, false); box.addeventlistener("drop", ondrop, false); var data = new formdata(); (var = 0; < files.length; i++) { data.append(files[i].name, files[i]); } $.ajax({ type: "post", url: "filehandler.ashx", contenttype: false, processdata: false, data: '{\'dhid\':\'' + $('#dhid').val() + '\', \'context\': \'' + data + '\'}', datatype:"json", success: function (result) { alert(result); }, error: function (xhr) { alert("there error uploading files!"); } }); }; function ondragenter(e) { e.stoppropagation(); e.preventdefault(); } function ondragover(e) { e.stoppropagation(); e.preventdefault(); } function ondrop(e) { e.stoppropagation(); e.preventdefault(); upload(e.datatransfer.files); }
});
okay, i've solved it.
i should pass dhid in url instead of data.
$.ajax({ type: "post", url: "filehandler.ashx?dhid=" + geturlparameter('dhid'), contenttype: false, processdata: false, data: data, success: function (result) { alert(result); }, error: function (xhr) { alert("there error uploading files!"); } });
Comments
Post a Comment