本文介绍了文件上传到FTP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 将文件上传到ftp服务器时出现异常。我正在执行以下代码并获取''System.Net.FtpWebRequest'的类型初始化程序引发异常。 private FtpWebRequest ftpRequest = null; private Stream ftpStream = null; private int bufferSize = 2048; protected void btnSubmit_Click( object sender,EventArgs e) { byte [] byteBuffer = new 字节 [BUFFERSIZE]; byte [] Data = fileArray.FileBytes; TransferFile(fileArray.PostedFile.FileName,Data)); } public string TransferFile( string filename, byte [] fileContent) { string strMsg = 串 .Empty; 尝试 { / * 创建FTP请求* / 字符串 uploadUrl = 字符串 .Format( {0} / {1} / {2}, ftp:// address, foldername,filename); ftpRequest =(FtpWebRequest)FtpWebRequest.Create( new Uri(uploadUrl)); / * 使用提供的用户名和密码登录FTP服务器* / ftpRequest.Credentials = new NetworkCredential( 用户名, 密码); / * 如有疑问,请使用以下选项* / ftpRequest.UseBinary = true ; ftpRequest.UsePassive = true ; ftpRequest.KeepAlive = true ; / * 指定FTP请求的类型* / ftpRequest.Method = WebRequestMethods.Ftp.UploadFile; ftpStream = ftpRequest.GetRequestStream(); / * 通过发送缓冲数据上传文件,直到传输为止完成* / 尝试 { ftpRequest.ContentLength = fileContent.Length; 流requestStream = ftpRequest.GetRequestStream(); requestStream.Write(fileContent, 0 ,fileContent.Length); requestStream.Close(); FtpWebResponse response =(FtpWebResponse)ftpRequest.GetResponse(); strMsg = 文件上传状态: + response.ToString(); } catch (Exception ex){Console.WriteLine(ex.ToString()); } / * 资源清理* / ftpStream.Close(); ftpRequest = null ; } catch (Exception ex){Console.WriteLine(ex.ToString()); } return strMsg; } 如果我做错了请建议。解决方案 您需要增加 FtpWebRequest.Timeout属性 [ ^ ]。 参考 - 底层连接是已关闭:接收时发生意外错误 [ ^ ]。 引用:只是想加强ScottE的诊断,并且更加具体。超时很可能是问题。 FtpWebRequest的.Net实现是错误的还是 MSDN文档有拼写错误,FtpWebRequest.Timeout的默认值不是-1(无限)。它是100000(100秒)。 此外还有另一个超时问题。一些测试表明,responseStream的超时值总是为300000(300秒)。我不知道如何分配这个值。无论如何,需要修改此值以适应大文件。 总之,解决方案是将FtpWebRequest.Timeout和Stream.Timeout设置为足够大的值 I got exception while upload file to ftp server. I am doing following code and get The type initializer for ''System.Net.FtpWebRequest'' threw an exception.private FtpWebRequest ftpRequest = null; private Stream ftpStream = null; private int bufferSize = 2048;protected void btnSubmit_Click(object sender, EventArgs e) { byte[] byteBuffer = new byte[bufferSize]; byte[] Data = fileArray.FileBytes; TransferFile(fileArray.PostedFile.FileName, Data)); }public string TransferFile(string filename, byte[] fileContent) { string strMsg = string.Empty; try { /* Create an FTP Request */ String uploadUrl = String.Format("{0}/{1}/{2}", "ftp://address", "foldername", filename); ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uploadUrl)); /* Log in to the FTP Server with the User Name and Password Provided */ ftpRequest.Credentials = new NetworkCredential("Username", "Password"); /* When in doubt, use these options */ ftpRequest.UseBinary = true; ftpRequest.UsePassive = true; ftpRequest.KeepAlive = true; /* Specify the Type of FTP Request */ ftpRequest.Method = WebRequestMethods.Ftp.UploadFile; ftpStream = ftpRequest.GetRequestStream(); /* Upload the File by Sending the Buffered Data Until the Transfer is Complete */ try { ftpRequest.ContentLength = fileContent.Length; Stream requestStream = ftpRequest.GetRequestStream(); requestStream.Write(fileContent, 0, fileContent.Length); requestStream.Close(); FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse(); strMsg = "File Upload Status: " + response.ToString(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } /* Resource Cleanup */ ftpStream.Close(); ftpRequest = null; } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return strMsg; }Please suggest if I do wrong. 解决方案 You need to increase the FtpWebRequest.Timeout Property [^].Refer - The underlying connection was closed: An unexpected error occurred on a receive[^].Quote:Just want to strengthen ScottE''s diagnosis, and be more specific. Timeout is most likely the issue.Either .Net implementation of FtpWebRequest is erroneous or the MSDN document has a typo, the default value of FtpWebRequest.Timeout is not -1 (Infinite). It is 100000 (100 seconds).In addition there is another timeout issue. A few tests have shown that responseStream always has a timeout value of 300000 (300 seconds). I do not how this value is assigned. Anyways, this value needs to be modified to accommodate large files.In summary, the solution is to set FtpWebRequest.Timeout and Stream.Timeout to a sufficiently large value. 这篇关于文件上传到FTP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!