如何在ASP MVC中将大文件上传到FTP服务器

时间:2018-06-06 20:25:13

标签: c# asp.net asp.net-mvc

我正在开发一个ASP MVC网站。 现在我需要将文件(.zip文件)上传到我的FTP服务器。要上传,请使用此代码。

此代码仅上传大小< 10 Mb。

例如:当我使用此代码上传150 MB大小的文件时,它会损坏,并且我的Ftp服务器上的文件大小更改为300 MB。

任何人都可以帮助我......

 byte[] fileBytes = null;

                //Read the FileName and convert it to Byte array.
                string filename = Path.GetFileName(FileUpload1.FileName);

                using (StreamReader fileStream = new StreamReader(FileUpload1.InputStream))
                {
                    fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd());
                    fileStream.Close();
                }

                try
                {
                    //Create FTP Request.
                    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + "/" + fileName);
                    request.Method = WebRequestMethods.Ftp.UploadFile;

                    //Enter FTP Server credentials.
                    request.Credentials = new NetworkCredential(ftpUName, ftpPWord);
                    request.ContentLength = fileBytes.Length;
                    request.UsePassive = true;
                    request.UseBinary = true;
                    request.ServicePoint.ConnectionLimit = fileBytes.Length;
                    request.EnableSsl = false;

                    using (Stream requestStream = request.GetRequestStream())
                    {
                        requestStream.Write(fileBytes, 0, fileBytes.Length);
                        requestStream.Close();
                    }

                    FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                    response.Close();
                }
                catch (WebException ex)
                {
                    throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
                }

2 个答案:

答案 0 :(得分:4)

将此添加到您的web.config

myFill = PatternFill(start_color='FF0000', 
                     end_color='FF0000', 
                     fill_type = 'solid')

ws.cell(row=row,column=column).fill = myFill

在system.web部分下 默认值为4 MB大小限制

Details here

答案 1 :(得分:1)

它可能会损坏,因为您正在使用utf8编码读取数据。你应该以二进制格式阅读它。

不要使用:

using (StreamReader fileStream = new StreamReader(FileUpload1.InputStream))
{
   fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd());
   fileStream.Close();
}

您必须使用 File.ReadAllBytes BinaryReader (流)

https://msdn.microsoft.com/en-us/library/system.io.file.readallbytes(v=vs.110).aspx

https://msdn.microsoft.com/de-de/library/system.io.binaryreader(v=vs.110).aspx

为您的例子:

   byte[] fileBytes = File.ReadAllBytes(Path.GetFileName(FileUpload1.FileName));
   try
   {
          //Create FTP Request.
          FtpWebRequest request = (FtpWebRequest)...