c# - Uploading a file using HTML-FileUpload-Control - Access Denied -
a click on import-button executes following jquery-code:
if ($("#fileupload").val()) { $.ajax({ async: false, url: "handler/handler.ashx?op=import", data: json.stringify(mydata), type: 'post', contenttype: 'application/json; charset=utf-8' });
mydata
json-object containing filepath , other values of html elements.
when statement reached...
using (var fs = new filestream(filepath, filemode.open, fileaccess.readwrite))
...this exception flies:
unauthorizedaccessexception - access path xxx denied.
i can solve problem giving full-control permissions iuser. however, not option, since our endusers can't expected modify permissions everytime want upload file. there way doesn't involve enduser input?
if understood correctly can give permission on fly, before using.
private bool grantaccess(string filepath) { fileinfo finfo = new fileinfo(filepath); filesecurity fsecurity = finfo.getaccesscontrol(); fsecurity.addaccessrule(new filesystemaccessrule("iuser", filesystemrights.fullcontrol, accesscontroltype.allow)); finfo.setaccesscontrol(fsecurity); return true; }
and after using-block
private bool removeaccess(string filepath) { fileinfo finfo = new fileinfo(filepath); filesecurity fsecurity = finfo.getaccesscontrol(); fsecurity.addaccessrule( new filesystemaccessrule("iuser", filesystemrights.fullcontrol, accesscontroltype.deny)); finfo.setaccesscontrol(fsecurity); return true; }
hope helps.
don't forget include using system.io;
Comments
Post a Comment