简体   繁体   English

从ASP.NET C#与MS Azure上的Blob存储交互

[英]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.

相关问题 Stream 视频来自 Azure blob 存储和 ASP.NET 核心 3 - Stream videos from Azure blob storage and ASP.NET Core 3 如何从asp.net C#中的Blob存储中显示pdf文件, - How to display pdf file from Blob storage in asp.net C#, Azure blob存储下载流返回“”asp.net - Azure blob storage download to stream returning “” asp.net 使用ASP.NET在Windows Azure blob存储上设置CORS - Set CORS on Windows Azure blob storage using ASP.NET 如何将图像从 Asp.net Core IFormFile 上传到 Azure Blob 存储? - How to Upload Image From Asp.net Core IFormFile to Azure Blob Storage? 如何使用ASP.NET MVC3在表中显示Azure Blob存储的所有图像? - How to display all the image from Azure Blob Storage in table using ASP.NET MVC3? 从 ASP.Net MVC 将 3+ GB 文件上传到 Azure Blob 存储的最佳方法是什么? - What is the best way to upload 3+ GB files to Azure Blob Storage from ASP.Net MVC? 如何在 ASP.NET Core 应用程序中显示来自 Azure Blob 存储帐户的图像? - How to I display an image from my Azure Blob Storage Account in my ASP.NET Core Application? C#没有从Azure blob存储中检索blob - C# not retrieving blob from Azure blob storage 如何使用.net SDK(C#)创建Azure搜索索引器以从Azure Blob存储中提取数据 - How to create an Azure Search Indexer using .net SDK (c#) to pull data from Azure blob storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM