Wednesday, 15 April 2015

c# - Not able to upload a file on server -



c# - Not able to upload a file on server -

i trying upload image.it works fine when localhost when published it throw error server:

when utilize code :

public string imagepath(httppostedfilebase imgfile) { var path = ""; // code saving image file physical location. var filename = path.getfilename(imgfile.filename); path = path.combine(httpcontext.server.mappath("~/images/sections/developer/clientlogo"), filename); string filenamewithoutextension = system.io.path.getfilenamewithoutextension(imgfile.filename); int iteration = 1; while (system.io.file.exists((path))) { filename = string.concat(filenamewithoutextension, "-", iteration, system.io.path.getextension(imgfile.filename)); path = path.combine(httpcontext.server.mappath("~/images/sections/developer/clientlogo"), filename); iteration++; } imgfile.saveas(path); // prepare relative path stored in database , used display later on. path = url.content(path.combine("~/images/sections/developer/clientlogo", filename)); homecoming path; }

error

system.unauthorizedaccessexception: access path 'd:\inetpub\vhosts\xx.com\httpdocs\images\sections\developer\clientlogo\circle-small-empty.18x18.png' denied. @ system.io.__error.winioerror(int32 errorcode, string maybefullpath) @ system.io.filestream.init(string path, filemode mode, fileaccess access, int32 rights, boolean userights, fileshare share, int32 buffersize, fileoptions options, security_attributes secattrs, string msgpath, boolean bfromproxy, boolean uselongpath, boolean checkhost) @ system.io.filestream..ctor(string path, filemode mode, fileaccess access, fileshare share, int32 buffersize, fileoptions options, string msgpath, boolean bfromproxy) @ system.io.filestream..ctor(string path, filemode mode) @ system.web.httppostedfile.saveas(string filename) @ system.web.httppostedfilewrapper.saveas(string filename) @ xx.corporatesite.controllers.developercontroller.imagepath(httppostedfilebase imgfile)

and when utilize server.mappath instead of httpcontext.server.mappath throw different error:

error is:

system.io.directorynotfoundexception: not find part of path 'd:\inetpub\vhosts\xx.com\httpdocs\images\sections\developer\clientlogo\demo.png'. @ system.io.__error.winioerror(int32 errorcode, string maybefullpath) @ system.io.filestream.init(string path, filemode mode, fileaccess access, int32 rights, boolean userights, fileshare share, int32 buffersize, fileoptions options, security_attributes secattrs, string msgpath, boolean bfromproxy, boolean uselongpath, boolean checkhost) @ system.io.filestream..ctor(string path, filemode mode, fileaccess access, fileshare share, int32 buffersize, fileoptions options, string msgpath, boolean bfromproxy) @ system.io.filestream..ctor(string path, filemode mode) @ system.web.httppostedfile.saveas(string filename) @ system.web.httppostedfilewrapper.saveas(string filename) @ xx.corporatesite.controllers.developercontroller.imagepath(httppostedfilebase imgfile)

i tried alter permission localhost nil working...please suggest me something

your web application not have permission write location you're trying save image. handled adding entry web.config points folder uploads saved

<appsettings> ... <add key="uploadpath" value="c:\uploads"/> ... </appsettings>

and in code read configuration entry identify path image saved:

.... string path = configurationmanager.appsettings["uploadpath"]; string filepath = path.combine(path, filename); ....

and in order save files directory, need set permissions on directory user web application running has write permissions on directory. allow write files web application folder.

this way, you, developer, not dictating files go. scheme administrator can decide both files go, , permissions necessary back upwards web application.

c# asp.net-mvc io

No comments:

Post a Comment