我是网络表单的 MVC 新手,我正在尝试将图像上传到我的 Azure 存储容器。 我已经可以根据需要上传和调整大小的图像,但是有些图像以错误的方向上传,例如,它们是横向上传的,而不是纵向上传的。
我已经用谷歌搜索过这个,但似乎找不到其他人有这个问题。
我必须上传的代码如下...
提前致谢
string accountName = "xxxxxxxxxxx";
string accountKey = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyy";
try
{
if (file != null)
{
StorageCredentials creds = new StorageCredentials(accountName, accountKey);
CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);
CloudBlobClient client = account.CreateCloudBlobClient();
CloudBlobContainer sampleContainer = client.GetContainerReference("uploadedimages");
sampleContainer.CreateIfNotExists();
CloudBlockBlob blob = sampleContainer.GetBlockBlobReference(file.FileName);
using (Stream file1 = file.InputStream)
{
WebImage img = new WebImage(file.InputStream);
if (img.Width > 300)
img.Resize(300, 300);
byte[] bytes = img.GetBytes();
Stream stream = new MemoryStream(bytes);
blob.UploadFromStream(stream);
}
string userIdValue = getUserId();
if (ModelState.IsValid)
{
YourDayEntities6 context = new YourDayEntities6();
context.CreateDayByDate(model.DayDate, blob.Uri.ToString(), model.DayDesc, userIdValue);
}
}
}
catch (Exception e)
{
throw new Exception(string.Format("Error{0}", e.Message));
}
return RedirectToAction("GetAllDaysByDate");
大多数时候,我看到这个问题不是由于存储引起的,Azure 存储将保存文件字节。
图像方向是包含在文件二进制文件中的EXIF信息的一部分。
如果您使用客户端 (JS) 库管理上传,您可以在其文档中查找,看看 EXIF 信息是否可能被它删除或修改,某些库甚至有更改它的方法。
手机拍摄的图像报告错误的EXIF信息(横向而不是纵向,反之亦然)存在一些问题,要解决此问题,您需要在上传之前处理EXIF信息。一些可能解决此问题的插件是blueimp Javascript Load Image,ng-file-upload for AngularJS。