从 ASP.NET C# 与 MS Azure 上的 blob 存储交互
interacting with blob storage on MS Azure from ASP.NET C#
我在一个网站上工作,我应该在完成开发后将它部署到 Azure 云上,问题是我的网络应用程序应该从用户本地计算机上传一个 blob 到 blob 存储
我的问题是,如何获取文件的文件路径?
我使用上传控件使用户能够上传文件
当用户单击提交按钮时,将执行代码以将上传控件中指定的文件上传到 blob 存储
如何在我的代码中获取文件路径变量的值?
文件是否需要上传到托管我的网络应用程序的服务器上,或者我可以只使用客户端本地计算机上的文件路径?
这是上传网络表单代码:
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);
}
}
}
}
查看 MSDN 上的此页面,尤其是示例:
这与您的情况类似。该示例将上传的文件保存到服务器,然后对其执行其他操作。但是,只要文件数据在内存中,您也可以使用 PostedFile 属性.
将文件数据写入 blob。
您将无法提供用户计算机上文件的文件路径。上传路径仅通过上传对话框提供;您无权从您的应用程序访问该路径。
我在一个网站上工作,我应该在完成开发后将它部署到 Azure 云上,问题是我的网络应用程序应该从用户本地计算机上传一个 blob 到 blob 存储 我的问题是,如何获取文件的文件路径? 我使用上传控件使用户能够上传文件 当用户单击提交按钮时,将执行代码以将上传控件中指定的文件上传到 blob 存储 如何在我的代码中获取文件路径变量的值? 文件是否需要上传到托管我的网络应用程序的服务器上,或者我可以只使用客户端本地计算机上的文件路径? 这是上传网络表单代码:
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);
}
}
}
}
查看 MSDN 上的此页面,尤其是示例:
这与您的情况类似。该示例将上传的文件保存到服务器,然后对其执行其他操作。但是,只要文件数据在内存中,您也可以使用 PostedFile 属性.
将文件数据写入 blob。您将无法提供用户计算机上文件的文件路径。上传路径仅通过上传对话框提供;您无权从您的应用程序访问该路径。