使用 asp.net web api 2 将媒体上传到 Azure Blob 存储
upload media to azure blob storage using asp.net web api 2
我正在开发将图像上传到我的 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 存储管理器并访问该容器时,我得到错误的格式,如下图所述
enter image description here
你能看到内容类型吗?我无法在资源管理器中打开此路径
任何帮助将不胜感激
您的 "filename" 来自以下代码的扩展名是否包含在您调试时(如 .jpg、.png)?例如"image.jpg"
var blob = blobContainer.GetBlockBlobReference(filename);
另外 "fileData.LocalFileName" 下面的代码需要有文件扩展名
using (var filestream = File.OpenRead(fileData.LocalFileName))
它没有扩展名,因此您遇到了这样的问题
我正在开发将图像上传到我的 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 存储管理器并访问该容器时,我得到错误的格式,如下图所述
enter image description here
你能看到内容类型吗?我无法在资源管理器中打开此路径 任何帮助将不胜感激
您的 "filename" 来自以下代码的扩展名是否包含在您调试时(如 .jpg、.png)?例如"image.jpg"
var blob = blobContainer.GetBlockBlobReference(filename);
另外 "fileData.LocalFileName" 下面的代码需要有文件扩展名
using (var filestream = File.OpenRead(fileData.LocalFileName))
它没有扩展名,因此您遇到了这样的问题