File uploaded to ftp server is corrupted c# -
i'm not sure why file corrupted. i'm not using streamreader has been common problem have had. uploads can't seem find issue on why file corrupted after searching through various solved stack overflow questions.
public actionresult uploadftpfile(httppostedfilebase promoimgfile) { bool issavedsuccessfully = true; string filename = null; string completedpath = "xxxxx"; string username = "xxxx"; string password = "xxxxx"; ftpwebrequest ftpclient; ftpwebrequest ftprequest; list<string> directories = new list<string>(); try { foreach (string fileitemname in request.files) { httppostedfilebase file = request.files[fileitemname]; filename = file.filename; if (file != null && file.contentlength > 0) { //create ftpwebrequest object ftpclient = (ftpwebrequest)ftpwebrequest.create(new uri("ftp://" + completedpath + "/" + filename)); //set request method ftpclient.method = webrequestmethods.ftp.uploadfile; //set credentials ftpclient.credentials = new networkcredential(username, password); //opens file read stream objfile = file.inputstream; //by default keepalive true, control connection not closed after command executed. ftpclient.keepalive = true; //set data transfer type ftpclient.usebinary = true; //set content length ftpclient.contentlength = file.contentlength; //get stream of file stream objstream = ftpclient.getrequeststream(); byte[] buffer = new byte[objfile.length]; //write file content objstream.write(buffer, 0, buffer.length); objstream.close(); objfile.close(); } } } catch(exception ex) { throw ex; issavedsuccessfully = false; } //return json(new { promoimgfile = directories }); return redirecttoaction("index", "home", new { promoimgfile = directories }); }
it's line right here:
stream objfile = file.inputstream;
years of learning, figured out 1 thing works:
memorystream ms = new memorystream(); file.inputstream.copyto(ms);
then work off memorystream.
Comments
Post a Comment