2011-07-25 135 views
5

對Windows Azure很新穎。我按照這個教程:tutorial。它的工作原理是完美的,但是對於我所考慮的應用程序而言,一個限制就是需要可以相對快速地上傳多個文件。將多個文件上傳到Azure Blob存儲

是否可以修改教程以支持多文件上傳,例如:用戶可以使用Shift-點擊來選擇多個文件。

或者如果有人知道任何好的教程詳述上述?

任何幫助表示讚賞,

感謝

回答

8

我會採取從DotNetCurry看看這個tutorial它展示瞭如何使用jQuery來處理文件的多個上傳到ASP創建多文件上傳.NET頁面。它是使用ASP.NET 3.5構建的,但是如果您使用.NET 4則無關緊要 - 沒有什麼太瘋狂的事情發生。

但關鍵是jQuery插件將允許您將一組文件上傳到服務器。在ASP.NET代碼後面將處理由通過Request.Files收集循環:

HttpFileCollection hfc = Request.Files; 
    for (int i = 0; i < hfc.Count; i++) 
    { 
     HttpPostedFile hpf = hfc[i]; 
     if (hpf.ContentLength > 0) 
     { 
      hpf.SaveAs(Server.MapPath("MyFiles") + "\\" + 
       System.IO.Path.GetFileName(hpf.FileName)); 
      Response.Write("<b>File: </b>" + hpf.FileName + " <b>Size:</b> " + 
       hpf.ContentLength + " <b>Type:</b> " + hpf.ContentType + " Uploaded Successfully <br/>"); 
     } 
    } 

你會將此代碼放在您的教程在insertButton_Click事件處理程序 - 基本上把一滴創建和上傳到Blob存儲上述內部代碼的if(hpf.ContentLength>0)塊。

所以僞代碼可能看起來像:

protected void insertButton_Click(object sender, EventArgs e) 
{ 
    HttpFileCollection hfc = Request.Files; 
    for (int i = 0; i < hfc.Count; i++) 
    { 
     HttpPostedFile hpf = hfc[i]; 

     // Make a unique blob name 
     string extension = System.IO.Path.GetExtension(hpf.FileName); 

     // Create the Blob and upload the file 
     var blob = _BlobContainer.GetBlobReference(Guid.NewGuid().ToString() + extension); 
     blob.UploadFromStream(hpf.InputStream); 

     // Set the metadata into the blob 
     blob.Metadata["FileName"] = fileNameBox.Text; 
     blob.Metadata["Submitter"] = submitterBox.Text; 
     blob.SetMetadata(); 

     // Set the properties 
     blob.Properties.ContentType = hpf.ContentType; 
     blob.SetProperties(); 
    } 
} 

再次,它只是僞代碼,所以我假定這是它是如何工作的。我沒有測試語法,但我認爲它很接近。

我希望這會有所幫助。祝你好運!

相關問題