简体   繁体   English

使用asp.net Web API 2将媒体上传到Azure Blob存储

[英]upload media to azure blob storage using asp.net web api 2

I'm developing rest web api that uploads images to my azure blob, I used one of the online tutorial and got this code 我正在开发将Web图片上传到我的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);
        }
    }

it is uploading the image in my account blob container but when i open azure storage manager and access the container i get wrong format as below picture describes 它正在将图像上传到我的帐户Blob容器中,但是当我打开azure存储管理器并访问该容器时,我得到了错误的格式,如下图所示

enter image description here 在此处输入图片说明

can u see the content-type ? 您可以看到内容类型吗? I'm not able open this path in explorer any help would be appreciated 我无法在资源管理器中打开此路径,将不胜感激

Is your "filename" from code below have extension included when you debuging (like .jpg, .png)? 在调试时,以下代码中的“文件名”是否包含扩展名(如.jpg,.png)? For example "image.jpg" 例如“ image.jpg”

var blob = blobContainer.GetBlockBlobReference(filename);

Also "fileData.LocalFileName" from code below need to have file extension 下面代码中的“ fileData.LocalFileName”也需要具有文件扩展名

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

It doesn't have extension and for this reason you have such issue 它没有扩展名,因此您有这样的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用ASP.NET Web API将图像添加到Azure blob存储失败 - Adding an image to Azure blob storage using ASP.NET Web API fails 通过 Web api ASP.NET 框架 Web 应用程序 C# 将图像/文件上传到 blob azure - Upload images/files to blob azure, via web api ASP.NET framework Web application c# 在asp.net中使用javascript在Azure BLOB存储上载期间,如何压缩视频? - How can i compress video during upload on Azure BLOB storage using javascript in asp.net? 使用ASP.NET在Windows Azure blob存储上设置CORS - Set CORS on Windows Azure blob storage using ASP.NET 如何从ASP.NET Web API删除Azure Blob? - How to delete Azure blob from asp.net web api? 在Azure上移动ASP.NET Web窗体站点,而无需再次对其进行编码以使用Azure Blob存储 - Moving an asp.net web forms site on azure without coding it again to use azure blob storage 如何将图像从 Asp.net Core IFormFile 上传到 Azure Blob 存储? - How to Upload Image From Asp.net Core IFormFile to Azure Blob Storage? 从 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? 使用 Web Api 将文件上传到 Azure blob - Upload files to Azure blob using Web Api 通过 web api 将大文件(&gt; 1 GB)上传到 azure blob 存储 - upload large files (> 1 GB) to azure blob storage through web api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM