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