2011-07-25 138 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(); 
    } 
} 

再次,它只是伪代码,所以我假定这是它是如何工作的。我没有测试语法,但我认为它很接近。

我希望这会有所帮助。祝你好运!

相关问题