2012-12-14 41 views
0

我在我的MVC3項目中使用uploadify。它可以很好地上傳多個文件並保存到文件夾。使用uploadify將上傳的文件路徑傳遞給控制器​​操作

如何將上傳文件的路徑傳遞給控制器​​操作? - 我需要將它傳遞給我的控制器的ExtractingZip動作。

要提取.zip文件的內容,我正在使用DotNetZip Library

這是我到目前爲止嘗試過的。

$('#file_upload').uploadify({ 
      'checkExisting': 'Content/uploadify/check-exists.php', 
      'swf': '/Content/uploadify/uploadify.swf', 
      'uploader': '/Home/Index', 
      'auto': false, 
      'buttonText': 'Browse', 
      'fileTypeExts': '*.jpg;*.jpeg;*.png;*.gif;*.zip', 
      'removeCompleted': false, 
      'onSelect': function (file) { 
       if (file.type == ".zip") { 
        debugger; 
        $.ajax({ 
         type: 'POST', 
         dataType: 'json', 
         url: '@Url.Action("ExtractingZip", "Home")', 
         data: ({ fileName: file.name}), // I dont see a file.path to pass it to controller 
         success: function (result) { 
          alert('Success'); 
         }, 
         error: function (result) { 
          alert('error'); 
         } 
        }); 
       } 

      } 
}); 

這裏是我的控制器操作:

[HttpPost] 
      public ActionResult ExtractingZip(string fileName,string filePath, HttpPostedFileBase fileData) 
      { 

       string zipToUnpack = @"C:\Users\Public\Pictures\Sample Pictures\images.zip";// I'm unable to get the filePath so i'm using the path. 
       string unpackDirectory = System.IO.Path.GetTempPath(); 

       using (ZipFile zip1 = ZipFile.Read(zipToUnpack)) 
       { 
        // here, we extract every entry, but we could extract conditionally 
        // based on entry name, size, date, checkbox status, etc. 
        var collections = zip1.SelectEntries("name=*.jpg;*.jpeg;*.png;*.gif;"); 

        foreach (var item in collections) 
        { 
         item.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently); 
        } 
       } 
       return Json(true); 
      } 

[HttpPost] 
     public ActionResult Index(IEnumerable<HttpPostedFileBase> fileData) 
     { 

       foreach (var file in fileData) 
       { 
        if (file.ContentLength > 0) 
        { 
         string currpath; 

         currpath = Path.Combine(Server.MapPath("~/Images/User3"), file.FileName); 
         //save to a physical location 
         file.SaveAs(currpath); 
        } 
       } 
     } 
+0

如何掛接是你uploadify? –

+0

不,我只是一個初學者uploadify。但是,如果我不使用'onSelect'在uploadify我可以讀取.zip文件數據與HttpPostedFileBase –

+0

所以你使用onselect嘗試使這是一個Ajax請求?我從來沒有使用過Uploadify,但我覺得ajax是默認的提交行爲。 –

回答

0

你不需要時,它的上傳到通過壓縮的文件路徑。文件路徑將來自客戶端機器嗎?您的服務器上的應用程序無法識別或訪問客戶端文件系統。

好消息是你不需要它。你已經有了你的文件在內存中的內容。我從來沒有使用過donetzip,但是一些Google的快速搜索表明,您可以直接從流中讀取zip文件。

請查看以下鏈接:

Cannot read zip file from HttpInputStream using DotNetZip 1.9

Extracting zip from stream with DotNetZip

因此,使用這些帖子爲基地,走下車的......它看起來像你應該能夠改變,像這樣的代碼:

  string zipToUnpack = @"C:\Users\Public\Pictures\Sample Pictures\images.zip";// I'm unable to get the filePath so i'm using the path. 
      string unpackDirectory = System.IO.Path.GetTempPath(); 

      using (ZipFile zip1 = ZipFile.Read(zipToUnpack)) 
      { 
       .... 

更改...

  string unpackDirectory = System.IO.Path.GetTempPath(); 

      using (ZipFile zip1 = ZipFile.Read(fileData.InputStream)) 
      { 
       .... 

讓我知道是否有幫助。

+0

即使我無法獲得'fileData'。它包含'null' –

+0

將此添加到您的控制器操作var fileUpload = Request.Files [0];它有你的文件數據嗎? –

+0

對不起本。即使我以前使用Request.Files [0]嘗試過,它並沒有返回任何結果。 –

0

首先,由於安全原因您無法直接訪問客戶機。當用戶上傳一些文件時,Web瀏覽器會根據RFC流或服務器端腳本讀取該流,因此不要浪費時間直接從用戶本地計算機的文件路徑獲取文件。

提取檔案(s.a:Zip,Rar)我強烈建議您使用SevenZipSharp。使用Streams和許多壓縮格式,它的工作非常簡單。

由於他們的文檔,你可以提取流是這樣的:

using (MemoryStream msin = new MemoryStream(fileData.InputStream)) 
{ ... } 
+0

謝謝。我可以只用SevenZipSharp提取指定的格式嗎?我的意思是如果.zip文件包含所有圖像。我只需要獲取.jpg文件。那可能嗎? –

+0

@Karthik要做到這一點,你應該搜索檔案文件的集合。 SevenZip.SevenZipExtractor.ArchiveFileData屬性爲您提供了一組文件名。您可以蒐集收藏內容並選擇要提取的文件(下載並閱讀文檔)。 – Alireza

+0

我沒有找到[Codeplex]上的SevenZipSharp的任何文檔(http://sevenzipsharp.codeplex.com/documentation) –

相關問題