[英]interacting with blob storage on MS Azure from ASP.NET C#
I am working on a website, I should deploy this on Azure cloud after I finish development, the thing is that my web app should upload a blob from the user local machine to the blob storage my question is , how do I get the file path of the file? 我在一个网站上工作,完成开发后应将其部署在Azure云上,问题是我的Web应用程序应将用户本地计算机上的Blob上传到Blob存储,我的问题是,如何获取文件路径文件? I use upload control to enable the user to upload a file when the user clicks the submit button there is code executed to upload the file specified in the upload control to the blob storage how can I get the value of the filepath variable in my code? 我使用上载控件使用户能够在用户单击“提交”按钮时上载文件,并且执行了将上载控件中指定的文件上载到Blob存储区的代码,如何在代码中获取filepath变量的值? does the file need to be uploaded on the server that is hosting my web app, or I can just use the path of the file from the client's local machine? 是否需要将文件上传到托管我的Web应用程序的服务器上,或者我可以只使用客户端本地计算机上文件的路径? this is the upload web form code: 这是上传网络表单代码:
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);
}
}
}
}
Take a look at this page on MSDN, particularly the example: 看一下MSDN上的此页面,特别是示例:
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.postedfile(v=vs.110).aspx https://msdn.microsoft.com/zh-CN/library/system.web.ui.webcontrols.fileupload.postedfile(v=vs.110).aspx
This is a similar scenario to yours. 这与您的情况类似。 The example saves the uploaded file to the server, then performs additional operations on it. 该示例将上载的文件保存到服务器,然后在其上执行其他操作。 However, you may also be able to write the file data to the blob so long as the file data is in memory, using the PostedFile property. 但是,只要文件数据在内存中,就可以使用PostFile属性将文件数据写入Blob。
You won't be able to provide the file path to the file on the user's machine. 您将无法提供用户计算机上文件的文件路径。 The upload path is provided through the upload dialog only; 上传路径仅通过上传对话框提供; you don't have access to that path from your application. 您将无法从应用程序访问该路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.