6

上傳可變大小的文件(對於ASP.NET MVC 2應用程序文件系統來說非常大或非常小)的最佳方法是什麼?上傳ASP.NET MVC 2文件的最佳方式是什麼?

這是迄今爲止我是這麼理解:

好像有兩種方式,人們處理這個問題。 (假設文件可能非常大或非常小)

(1)通過Request.FilesHttpPostedFileBase處理控制器操作中的上傳,這似乎具有花費很長時間的缺點,因爲ASP.NET加載文件進入活動內存。

(2)攔截文件上傳早與這在某種程度上規避性能問題的HTTP模塊。 (我對這項工作有點多雲,但我一直在使用這篇文章http://darrenjohnstone.net/2008/07/15/aspnet-file-upload-module-version-2-beta-1/作爲參考)。 我很模糊的部分是ASP.NET在什麼時候將提交的文件加載到活動內存,以及如何在模塊中攔截它實際上改變了這種行爲。

由於第二種選擇更快,它似乎是更好的選擇。但是似乎提交上傳表單的應用程序可能會有一些與需要在數據庫中保存的文件相關的數據。我不想在我的HttpHandler或HttpModule中進行持久性調用(因爲那時我會在不同的地方發生兩個非常類似的功能:控制器和http處理程序)。

我想一個解決方法是將目標文件的位置存儲在HttpContext.Items中,但是是最好的方法嗎?

最後一個問題是我想在文件完成上傳之前呈現HttpResponse。所以,如果有一個大文件,我會向用戶發送一個具有上傳狀態值的視圖,並使AJAX調用保持狀態更新。 如何在保持上傳過程的同時呈現結果?我需要製作一個AsyncHandler或AsyncController嗎?我是否需要手動抓取另一個線程?

非常感謝。我知道這是很多問題,並且可能反映了對某事的普遍缺乏理解。關於普遍缺乏理解的有趣之處在於,擁有它們的人們往往缺乏對缺乏理解的理解......因此,如果任何人都可以在該說明中指出我正確的方向,我將不勝感激。

+0

[File upload MVC](http://stackoverflow.com/questions/765211/file-upload-mvc) – jgauffin 2011-01-25 05:55:57

+0

的可能重複你在開玩笑嗎? – smartcaveman 2011-01-25 15:48:10

+0

@jguaffin - 您鏈接到的問題根本與此問題無關。 – Fenton 2011-01-31 15:20:31

回答

0

我用這個JavaScript tool

這是控制器(我仔細檢查原因IE瀏覽器有一個奇怪的行爲):

<HttpPost()> _ 
Function UploadExcelPriceList(ByVal id As String) As System.String 

    Dim bResult As Boolean = False 
    Dim IsIE As Boolean = False 
    Dim sFileName As String = "" 

    If (Request.Files Is Nothing) OrElse (Request.Files.Count = 0) Then 
     If String.IsNullOrEmpty(Request.Params("qqfile")) Then 
      Return ("{success:false, error:'request file is empty'}") 
     Else 
      sFileName = Request.Params("qqfile").ToString 
     End If 
    Else 
     sFileName = Request.Files(0).FileName 
     IsIE = True 
    End If 

    If String.IsNullOrEmpty(sFileName) Then 
     Return ("{success:false, error:'request file is empty'}") 
    End If 

    Dim DocumentName As String = Id & Path.GetExtension(sFileName) 

    If IsIE Then 
     Try 
      Request.Files(0).SaveAs(Path.Combine(My.Settings.TempFolder, DocumentName)) 
     Catch ex As Exception 
      Return ("{success:false, error:'" & ex.Message & "'}") 
     End Try 
    Else 
     Try 
      If (Request.InputStream IsNot Nothing) AndAlso (Request.InputStream.CanRead) AndAlso (Request.InputStream.Length > 0) Then 
       Using fileStream As FileStream = New FileStream(Path.Combine(My.Settings.TempFolder, DocumentName), FileMode.Create) 
        Dim FileBytes(Core.Convert.ToInt32(Request.InputStream.Length)) As Byte 
        Dim bytesRead As Int32 = 0 
        bytesRead = Request.InputStream.Read(FileBytes, 0, FileBytes.Length) 
        fileStream.Write(FileBytes, 0, bytesRead) 
        fileStream.Flush() 
        fileStream.Close() 
        bytesRead = Nothing 
       End Using 
      End If 
     Catch ex As Exception 
      Return ("{success:false, error:'" & ex.Message & "'}") 
     End Try 
    End If 

    Return ("{success:true, id: '" & Id & "'}") 

End Function 

我把這個HTML筆者認爲:

<div id="PopupExcelUploader" title="Carica Listino Excel"> 
    <div id="uploaderFile"></div> 
</div> 

這是javascript:

function CreateFileUploader() { 
    var uploader = new qq.FileUploader({ 
     element: $('#uploaderFile')[0], 
     template: '<div class="qq-uploader">' + 
           '<div class="qq-upload-drop-area"><span>Drop files here to upload</span></div>' + 
           '<div class="qq-upload-button ui-button ui-widget ui-corner-all ui-button-text-only ui-state-default">Seleziona il Listino Excel</div>' + 
           '<ul class="qq-upload-list"></ul>' + 
           '</div>', 
     hoverClass: 'ui-state-hover', 
     focusClass: 'ui-state-focus', 
     action: UploaderAction, 
     allowedExtensions: ['xls', 'xlsx'], 
     params: { id: ModelId }, 
     onSubmit: function(file, ext) { 
     }, 
     onComplete: function(id, fileName, responseJSON) { 
      if ((responseJSON.success == null) || (responseJSON.success == 'false')) { 
       $.jGrowl("Error!", { theme: 'MessageError', life: 3000 }); 
      } 
      else { 
       documentUploaded = true; 
       $.jGrowl("Document uploaded successfully!", { theme: 'MessageOk', life: 1800 }); 
       window.setTimeout(function() { 
        $("#PopupExcelUploader").dialog('close'); 
        $("#PriceListDynamicGrid").trigger("reloadGrid"); 
       }, 3000); 
      } 
     } 
    }); 
} 
相關問題