c#ftp上传大文件异常 [英] c# ftp upload large files exception

查看:275
本文介绍了c#ftp上传大文件异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件从一台服务器上传到另一台服务器

小文件(< 30MB)一切正常,但当我尝试上传大文件时,会出现错误 连接已关闭:接收时发生意外错误



这是我的代码:



Hi, I am trying to upload files from one server to another
Everything works fine with small files(< 30MB), but when I try to upload a large file, an error appears "The connection was closed: An unexpected error occurred on a receive"

this is my code:

try
        {
            string dir = ConfigurationManager.AppSettings["ftp"].ToString();
            string user = ConfigurationManager.AppSettings["ftpUser"].ToString();
            string pass = ConfigurationManager.AppSettings["ftpPass"].ToString();

            //SUBIR

            DirectoryInfo directory = new DirectoryInfo(Server.MapPath("/documentos/" + clave + "/"));
            FileInfo[] files = directory.GetFiles("*.pdf");
			
			FtpWebRequest requestFTPUploader = (FtpWebRequest)WebRequest.Create(dir + "/" + "PANDEMO" + clave + "/");
            requestFTPUploader.Credentials = new NetworkCredential(user, pass);
            requestFTPUploader.Method = WebRequestMethods.Ftp.MakeDirectory;
            StreamReader reader = new StreamReader(requestFTPUploader.GetResponse().GetResponseStream());
            
            for (int i = 0; i < files.Length; i++)
            {
                requestFTPUploader = (FtpWebRequest)WebRequest.Create(dir + "/" + "PANDEMO" + clave + "/" + files[i].Name);
                requestFTPUploader.Credentials = new NetworkCredential(user, pass);
                requestFTPUploader.UsePassive = true;
                requestFTPUploader.UseBinary = true;
                requestFTPUploader.KeepAlive = true;
                requestFTPUploader.Method = WebRequestMethods.Ftp.UploadFile;
                requestFTPUploader.Timeout = -1;

                reader = new StreamReader(requestFTPUploader.GetResponse().GetResponseStream());

                FileInfo fileInfo = new FileInfo(Server.MapPath("/documentos/" + clave + "/" + files[i].Name));
                FileStream fileStream = fileInfo.OpenRead();

                int bufferLength = 2048;
                byte[] buffer = new byte[bufferLength];

                Stream uploadStream = requestFTPUploader.GetRequestStream();
                int contentLength = fileStream.Read(buffer, 0, bufferLength);
                uploadStream.WriteTimeout = -1;
                uploadStream.ReadTimeout = -1;
                while (contentLength != 0)
                {
                    uploadStream.Write(buffer, 0, contentLength);
                    contentLength = fileStream.Read(buffer, 0, bufferLength);
                }
                uploadStream.Close(); 
                fileStream.Close();

                

                requestFTPUploader = null;
            }
        }
        catch (Exception err)
        {
            ShowMssg(err.Message);

        }





另外,在web.config我用过:



also, in the web.config I used this:

<security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="314572800"/>
      </requestFiltering>
    </security>
<system.web>
    <httpRuntime maxRequestLength="307200" executionTimeout="100000"/>
...





有人知道为什么只发生大文件?



Does someone knows why Only happens with large files?

推荐答案

由于它是一个大文件,操作系统会以块的形式发送它。当你关闭它时,缓冲区中可能还有一些字节。在关闭流之前尝试uploadStream.Flush(),先将最后一个字节写入流中。
Since its a large file the OS will send it in chunks. Probably there is still some bytes in the buffer when you go to close it. Try uploadStream.Flush() before closing the stream to write the last of the bytes to the stream first.


这篇关于c#ftp上传大文件异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆