我正在开发将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);
}
}
它是在我的帐户blob容器中上传图像但是当我打开azure存储管理器并访问容器时我得到的格式错误如下图所示
你能看到内容类型吗?我无法在资源管理器中打开这条路径 任何帮助将不胜感激答案 0 :(得分:0)
你的&#34;文件名&#34;从以下代码中进行调试时包含扩展名(如.jpg,.png)?例如&#34; image.jpg&#34;
var blob = blobContainer.GetBlockBlobReference(filename);
还&#34; fileData.LocalFileName&#34;从下面的代码需要有文件扩展名
using (var filestream = File.OpenRead(fileData.LocalFileName))
它没有扩展名,因此你有这样的问题