gpt4 book ai didi

c# - 将多个文件上传到 Azure Blob 存储

转载 作者:太空狗 更新时间:2023-10-29 21:19:23 26 4
gpt4 key购买 nike

Windows Azure 的新手。我遵循了本教程:tutorial .它工作完美,但一个限制是对于我想到的应用程序,需要能够相对快速地上传多个文件。

是否可以修改教程以支持多文件上传,例如用户可以使用 shift-click 来选择多个文件..

或者如果有人知道任何详细说明上述内容的好教程?

感谢任何帮助,

谢谢

最佳答案

我会看看这个 tutorial来自 DotNetCurry,它展示了如何使用 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 创建和上传到 blob 存储放在上面代码的 if(hpf.ContentLength>0) block 。

所以伪代码可能看起来像:

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();
}
}

同样,这只是伪代码,所以我假设它是这样工作的。我没有测试语法,但我认为它很接近。

希望对您有所帮助。祝你好运!

关于c# - 将多个文件上传到 Azure Blob 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6821162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com