【问题标题】:interacting with blob storage on MS Azure from ASP.NET C#从 ASP.NET C# 与 MS Azure 上的 blob 存储交互
【发布时间】: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


    【解决方案1】:

    看看MSDN上的这个页面,特别是例子:

    https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.postedfile(v=vs.110).aspx

    这与您的情况类似。该示例将上传的文件保存到服务器,然后对其执行附加操作。但是,只要文件数据在内存中,您也可以使用 PostedFile 属性将文件数据写入 Blob。

    您将无法提供用户计算机上文件的文件路径。上传路径仅通过上传对话框提供;您无权从您的应用程序访问该路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-09
      • 1970-01-01
      • 1970-01-01
      • 2021-05-06
      • 1970-01-01
      • 2017-08-12
      • 2014-05-04
      相关资源
      最近更新 更多