0

我正在开发将图像上传到我的 azure blob 的 rest web api,我使用了其中一个在线教程并获得了此代码

 public class DocumentsController : ApiController
{
    private const string CONTAINER = "documents";

    // POST api/<controller>
    public async Task<HttpResponseMessage> Post()
    {
        var context = new StorageContext();

        // Check if the request contains multipart/form-data.
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        // Get and create the container
        var blobContainer = context.BlobClient.GetContainerReference(CONTAINER);
        blobContainer.CreateIfNotExists();

        string root = HttpContext.Current.Server.MapPath("~/App_Data");
        var provider = new MultipartFormDataStreamProvider(root);

        try
        {
            // Read the form data and return an async task.
            await Request.Content.ReadAsMultipartAsync(provider);

            // This illustrates how to get the file names for uploaded files.
            foreach (var fileData in provider.FileData)
            {
                var filename = fileData.LocalFileName;
                var blob = blobContainer.GetBlockBlobReference(filename);

                using (var filestream = File.OpenRead(fileData.LocalFileName))
                {
                    blob.UploadFromStream(filestream);
                }
                File.Delete(fileData.LocalFileName);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }
        catch (System.Exception e)
        {
            return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
        }
    }

它正在我的帐户 blob 容器中上传图像,但是当我打开 azure 存储管理器并访问容器时,我得到错误的格式,如下图所示

在此处输入图像描述

你能看到内容类型吗?我无法在资源管理器中打开此路径任何帮助将不胜感激

4

1 回答 1

0

调试时是否包含以下代码中的“文件名”扩展名(如 .jpg、.png)?例如“image.jpg”

var blob = blobContainer.GetBlockBlobReference(filename);

下面代码中的“fileData.LocalFileName”也需要有文件扩展名

using (var filestream = File.OpenRead(fileData.LocalFileName))

它没有扩展名,因此你有这样的问题

于 2018-05-18T14:14:00.830 回答