【问题标题】:How to upload a file posted in REST API to Azure Storage如何将 REST API 中发布的文件上传到 Azure 存储
【发布时间】:2018-12-16 09:43:47
【问题描述】:

我需要能够在 REACT UI 中选择一个文件,然后它将以发布的文件作为参数之一调用 REST 服务,然后上传到 Azure 存储

我有这样的事情:

  public async Task<IHttpActionResult>  PutTenant(string id, Tenant tenant, HttpPostedFile certificateFile)
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureStorageKey"].ToString());
            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve reference to a previously created container.
            CloudBlobContainer container = blobClient.GetContainerReference(ConfigurationManager.AppSettings["certificatesContainer"].ToString());

            // Retrieve reference to a blob named "myblob".
            CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");

            // Create or overwrite the "myblob" blob with contents from a local file.
            using (var fileStream = System.IO.File.OpenRead(certificateFile))
            {
                blockBlob.UploadFromStream(fileStream);
            }

            var tenantStore = CosmosStoreFactory.CreateForEntity<Tenant>();
            tenant.CertificatePath = blockBlob.Uri;

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            if (id != tenant.TenantId)
            {
                return BadRequest();
            }

            var added = await tenantStore.AddAsync(tenant);
            return StatusCode(HttpStatusCode.NoContent); 
        }

openread 行是我不确定的地方,如何获取 HttpPostedFile 然后上传到 Azure 存储。

【问题讨论】:

    标签: c# .net asp.net-web-api asp.net-web-api2 azure-storage


    【解决方案1】:

    你应该使用内置的HttpPostedFile.InputStream直接上传到blob,并设置blob的文件类型。

    将您的 using (var fileStream 块替换为以下内容:

    blockBlob.Properties.ContentType = certificateFile.ContentType;
    blockBlob.UploadFromStream(certificateFile.InputStream);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-08
      • 2021-11-06
      • 1970-01-01
      • 2016-10-04
      • 2021-11-05
      • 2019-08-13
      • 2014-05-14
      相关资源
      最近更新 更多