c# - Access to the path is denied - different from user -
i have web page in asp.net & c#. page show table our orders. suppliers need check data , save them.
my problem : when suppliers click on "save", pdf downloaded. have more 100 suppliers use website, works 98% of our suppliers. 3 suppliers have error message when click on "save" :
access path "c:\externedata\pdf\f000001.pdf" denied.
this code used access pdf :
// save document... string filename = server.mappath("~/pdf/" + url_supplierid + ".pdf"); document.save(filename); string path = filename; string name = url_supplierid + ".pdf"; filestream fs = new filestream(path, filemode.open, fileaccess.read); // create byte array of file stream length byte[] _downfile = new byte[fs.length]; //read block of bytes stream byte array fs.read(_downfile, 0, system.convert.toint32(fs.length)); //close file stream fs.close(); session["pdfcontrol"] = _downfile; session["pdfcontrolname"] = url_supplierid + "_" + url_purchid + ".pdf"; if (file.exists(filename)) file.delete(filename); byte[] _downfile2 = session["pdfcontrol"] byte[]; session["pdfcontrol"] = null; response.clear(); response.clearcontent(); response.clearheaders(); response.contenttype = "application/pdf"; response.addheader("content-disposition", "attachment; filename=" + session["pdfcontrolname"] + "; size=" + _downfile2.length.tostring()); response.binarywrite(_downfile2); response.flush(); response.end();
the thing don't understand message show me access right error. works me , 98% of our suppliers. error doesn't come server ?
if you've got permissions correct, think can think of try different way (do these suppliers have bigger pdfs others?). don't see need create 2 byte arrays or use session variables here. maybe this:
// save document... string filename = server.mappath("~/pdf/" + url_supplierid + ".pdf"); document.save(filename); using (filestream fs = new filestream(filename, filemode.open, fileaccess.read)) { // create byte array of file stream length byte[] _downfile = new byte[fs.length]; int numbytestoread = (int)fs.length; int numbytesread = 0; //read block of bytes stream byte array while (numbytestoread > 0) { // read may return 0 numbytestoread. int n = fs.read(_downfile, numbytesread, numbytestoread); // break when end of file reached. if (n == 0) break; numbytesread += n; numbytestoread -= n; } numbytestoread = _downfile.length; } if (file.exists(filename)) file.delete(filename); response.clear(); response.clearcontent(); response.clearheaders(); response.contenttype = "application/pdf"; response.addheader("content-disposition", "attachment; filename=" + path.getfilename(filename) + "; size=" + numbytestoread); response.binarywrite(_downfile); response.flush(); response.end();
Comments
Post a Comment