【发布时间】:2016-09-13 13:24:54
【问题描述】:
我正在开发一个网站,我应该在完成开发后将它部署在 Azure 云上,问题是我的 Web 应用程序应该将 Blob 从用户本地计算机上传到 Blob 存储 我的问题是,如何获取文件的文件路径? 我使用上传控件使用户能够上传文件 当用户单击提交按钮时,将执行代码以将上传控件中指定的文件上传到 blob 存储 如何在我的代码中获取文件路径变量的值? 该文件是否需要上传到托管我的 Web 应用程序的服务器上,或者我可以只使用来自客户端本地计算机的文件路径? 这是上传网页表单代码:
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.Azure;
namespace myProject
{
public partial class popout : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
string filePath;
if (FileUpload1.PostedFile != null)
{
filePath = FileUpload1.PostedFile.FileName;
}
else { filePath = null; }
// file name with path of the file uploaded using FileUpload1 control
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("connectionstringWhatever"));
// Create a blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Get a reference to a container named “myContainer”
CloudBlobContainer container = blobClient.GetContainerReference("myContainer");
//setting the permission to public
container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
// Retrieve reference to a blob named "blob1".or if doesn't exist, create one
CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1");
if (filePath != null)
{
using (var fileStream = System.IO.File.OpenRead(filePath))
{
blockBlob.UploadFromStream(fileStream);
}
}
else {
string msg = "FileUpload1 = null";
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + msg + "');", true);
}
}
}
}
【问题讨论】:
标签: c# asp.net azure-blob-storage