2016-03-11 5 views

ответ

0

Добавить ниже в файл web.config,

<configuration> 
    <system.web> 
     <httpRuntime maxRequestLength="1048576" /> 
    </system.web> 
</configuration> 
+0

Я знаю, как увеличить maxRequestLength; мой ответ: если можно увеличить верхние 4 ГБ –

+0

О, странно, чтобы загрузить такой большой файл. Во всяком случае, я об этом не знаю. –

+0

Да ... для меня единственным решением является разделение файлов или использование FTP ... –

1

Вы можете попробовать автоматически разрезать большой файл на стороне клиента, чем сливать его на стороне сервера. Вот пример кода для MVC: http://forums.asp.net/t/1742612.aspx?How+to+upload+a+big+file+in+Mvc+

стороне клиента:

var uploadComplete = function() { 
     var formData = new FormData(); 
     formData.append('fileName', file.name); 
     formData.append('completed', true); 

     var xhr2 = new XMLHttpRequest(); 
     xhr2.open("POST", "/Files/UploadComplete", true); //combine the chunks together 
     xhr2.send(formData); 
    } 

    function upload(file) { 
     var blob = file; 
     var BYTES_PER_CHUNK = 77570; // sample chunk sizes. 
     var SIZE = blob.size; 

     //upload content 
     var start = 0; 
     var end = BYTES_PER_CHUNK; 
     var completed = 0; 
     var count = SIZE % BYTES_PER_CHUNK == 0 ? SIZE/BYTES_PER_CHUNK : Math.floor(SIZE/BYTES_PER_CHUNK) + 1; 

     while (start < SIZE) { 
      var chunk = blob.webkitSlice(start, end); 
      var xhr = new XMLHttpRequest(); 
      xhr.onload = function() { 
       completed = completed + 1; 
       if (completed === count) { 
        uploadComplete(); 
       } 
      }; 
      xhr.open("POST", "/Files/MultiUpload", true); 
      xhr.send(chunk); 

      start = end; 
      end = start + BYTES_PER_CHUNK; 
     } 
    } 

стороне сервера:

[HttpPost] 
    public string MultiUpload() 
    { 
     var chunks = Request.InputStream; 

     string path = Server.MapPath("~/App_Data/Uploads/Tamp"); 
     string newpath = Path.Combine(path, Path.GetRandomFileName()); 

     using (System.IO.FileStream fs = System.IO.File.Create(newpath)) 
     { 
      byte[] bytes = new byte[77570]; 

      int bytesRead; 
      while ((bytesRead = Request.InputStream.Read(bytes, 0, bytes.Length)) > 0) 
      { 
       fs.Write(bytes, 0, bytesRead); 
      } 
     } 
     return "test"; 
    } 

    [HttpPost] 
    public string UploadComplete(string fileName, bool completed) 
    { 
     if (completed) 
     { 
      string path = Server.MapPath("~/App_Data/Uploads/Tamp"); 
      string newpath = Path.Combine(path, fileName); 
      string[] filePaths = Directory.GetFiles(path); 

      foreach (string item in filePaths) 
      { 
       MergeFiles(newpath, item); 
      } 
     } 
     return "success"; 
    } 

    private static void MergeFiles(string file1, string file2) 
    { 
     FileStream fs1 = null; 
     FileStream fs2 = null; 
     try 
     { 
      fs1 = System.IO.File.Open(file1, FileMode.Append); 
      fs2 = System.IO.File.Open(file2, FileMode.Open); 
      byte[] fs2Content = new byte[fs2.Length]; 
      fs2.Read(fs2Content, 0, (int)fs2.Length); 
      fs1.Write(fs2Content, 0, (int)fs2.Length); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message + " : " + ex.StackTrace); 
     } 
     finally 
     { 
      fs1.Close(); 
      fs2.Close(); 
      System.IO.File.Delete(file2); 
     } 
    } 
+0

Спасибо, Лесмиан! –