c# - webclient.UploadData "The underlying Connection was Closed: An unexpected error occured on a recieve" -
i trying upload image , video using code below, there no issue image upload using ftp, when uploading video following error
error
the underlying connection closed: unexpected error occurred on receive.
following code using upload
code
try { string uploadfilename = path.getfilename(fu_video.filename); uploadfilename = "video." + uploadfilename.split('.')[1]; using (webclient client = new webclient()) { string ftpaddres = "ftp://username:pasword@url-path" + fullname; if (!getallfileslist(ftpaddres, "username", "password")) { ftpwebrequest ftp = (ftpwebrequest)ftpwebrequest.create(ftpaddres); ftp.method = webrequestmethods.ftp.makedirectory; ftp.keepalive = false; ftpwebresponse response = (ftpwebresponse)ftp.getresponse(); } client.uploaddata(new uri(ftpaddres + "/" + uploadfilename), fu_video.filebytes); }
and here stack trace.
stack trace
@ system.net.webclient.uploaddatainternal(uri address, string method, byte[] data, webrequest& request) @ system.net.webclient.uploaddata(uri address, string method, byte[] data) @ system.net.webclient.uploaddata(uri address, byte[] data) @ mentormentee.signup.signup.btn_vidpreview_click(object sender, eventargs e) in i:\vs projects\code\mentormentee1\mentormentee\signup\signup.aspx.cs:line 469
after searching read make connection alive false, getting error on line client.uploaddata(uri,byte[]);
please let me know wrong code? video uploaded on ftp, error.
i remember having similar issue, don't remember made work. here code works me:
public void upload(stream stream, string filename) { if (stream == null) { throw new argumentnullexception("stream"); } try { ftpwebrequest ftprequest = createftprequest(filename); ftprequest.method = webrequestmethods.ftp.uploadfile; using (stream requestsream = ftprequest.getrequeststream()) { pump(stream, requestsream); } var ftpresponse = (ftpwebresponse)ftprequest.getresponse(); ftpresponse.close(); } catch (exception e) { throw new ftpexception( string.format("failed upload object. filename: {0}, stream: {1}", filename, stream), e); } } private ftpwebrequest createftprequest(string filename) { if (filename == null) { throw new argumentnullexception("filename"); } string serveruri = string.format("{0}{1}", ftproot, filename); var ftprequest = (ftpwebrequest)webrequest.create(serveruri); ftprequest.credentials = new networkcredential(configuration.username, configuration.password); ftprequest.usepassive = true; ftprequest.usebinary = true; ftprequest.keepalive = false; return ftprequest; } private static void pump(stream input, stream output) { var buffer = new byte[2048]; while (true) { int bytesread = input.read(buffer, 0, buffer.length); if (bytesread == 0) { break; } output.write(buffer, 0, bytesread); } }
Comments
Post a Comment