gpt4 book ai didi

c# - Azure存储,无法正确上传图片

转载 作者:行者123 更新时间:2023-11-30 17:59:42 25 4
gpt4 key购买 nike

我正在尝试在 mvc 项目中创建一个操作。可以将文件和图像上传到我的 azure 存储。但由于某种原因,它不会正确上传,这就是我的猜测。如果我使用“Azure Storage Explorere”上传图像,它就可以正常工作。示例:http://storage.sogaard.us/company1/wallpaper-396234.jpg但是,如果我尝试上传图像,但我的操作将不起作用,它会发送 200 表示成功和正确的内容类型,但图像将无法加载,并且我的开发人员工具告诉我,我没有从服务器获取数据。示例:http://storage.sogaard.us/company1/b9206edac188e1d8aa2b3be7cdc4b94a.jpg

我尝试将上传的 til 保存到本地计算机而不是 azure 存储,并且在那里工作正常!我根本找不到原因,这一直困扰着我:(

这是我的代码

[HttpPost]
public ActionResult Upload(FilesUploadModel model, IEnumerable<HttpPostedFileBase> files)
{

if(ModelState.IsValid)
{
if (files != null && files.Any())
{
int maxSizeInBytes = ConfigurationManager.Instance.Configuration.FileMaxSize;
Size thumbSize = new Size(200, 200);

foreach (HttpPostedFileBase file in files.Where(x => x != null))
{
CMS.Common.Data.File _file = new Sogaard.Inc.CMS.Common.Data.File();

// is any files uploadet?
if (!(file.ContentLength > 0))
{
FlashHelper.Add(Text("File not received"), FlashType.Error);
continue;
}
// is the file larger then allowed
if (file.ContentLength > maxSizeInBytes)
{
FlashHelper.Add(Text("The file {0}'s file size was larger then allowed", file.FileName), FlashType.Error);
continue;
}

var fileName = Encrypting.MD5(Path.GetFileNameWithoutExtension(file.FileName) + DateTime.Now) + Path.GetExtension(file.FileName);
string mimeType = FileHelper.MimeType(FileHelper.GetMimeFromFile(file.InputStream));

_file.SiteId = SiteId();
_file.Container = GetContainerName();
_file.FileName = Path.GetFileName(file.FileName);
_file.FileNameServer = fileName;
_file.Created = DateTime.Now;
_file.Folder = model.Folder;
_file.Size = file.ContentLength;
_file.Type = mimeType;

if (mimeType.ToLower().StartsWith("image/"))
{
try
{
// So we don't lock the file
using (Bitmap bitmap = new Bitmap(file.InputStream))
{
_file.Information = bitmap.Width + "|" + bitmap.Height;

if (bitmap.Height > 500 && bitmap.Width > 500)
{
var thumbfileName = Encrypting.MD5(Path.GetFileNameWithoutExtension(file.FileName) + "thumb" + DateTime.Now) + ".jpeg";
Size thumbSizeNew = BaseHelper.ResizeImage(bitmap.Size, thumbSize);
Bitmap thumbnail = (Bitmap)bitmap.GetThumbnailImage(thumbSizeNew.Width,
thumbSizeNew.Height,
ThumbnailCallback,
IntPtr.Zero);
_file.ThumbFileNameServer = thumbfileName;
// Retrieve reference to a blob named "myblob"
CloudBlob blob = container().GetBlobReference(_file.ThumbFileNameServer);
blob.Metadata["Filename"] = Path.GetFileNameWithoutExtension(file.FileName) + "-thumb" + ".jpg";
blob.Properties.ContentType = "image/jpeg";

// Create or overwrite the "myblob" blob with contents from a local file
using (MemoryStream memStream = new MemoryStream())
{
thumbnail.Save(memStream, ImageFormat.Jpeg);
blob.UploadFromStream(memStream);
}
blob.SetMetadata();
blob.SetProperties();
}
}
}
catch (Exception e)
{
if (e.GetType() != typeof (DataException))
FlashHelper.Add(Text("The image {0} was not a valid image", file.FileName),
FlashType.Error);
// Removing the file
System.IO.File.Delete(file.FileName);
}
}
else
{
_file.Information = null;
}
// Retrieve reference to a blob named "myblob"
CloudBlob blobF = container().GetBlobReference(fileName);
blobF.Metadata["Filename"] = file.FileName;
blobF.Properties.ContentType = mimeType;

// Create or overwrite the "myblob" blob with contents from a local file
blobF.UploadFromStream(file.InputStream);
blobF.SetMetadata();
blobF.SetProperties();

fileService.Save(_file);
}
}

return RedirectToAction("Display", new { Folder = model.Folder });
}

model.FolderSugestion = fileService.GetFolders(SiteId());
return View(model);
}

private CloudBlobContainer container()
{
// Retrieve storage account from connection-string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));

// Create the blob client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container
var container = blobClient.GetContainerReference(GetContainerName());
container.CreateIfNotExist();
return container;
}

private string GetContainerName()
{
return "company" + SiteId();
}

最佳答案

在缩略图代码路径中,我认为您需要 memStream.Position = 0 在尝试上传之前将流重置回开头。

对于其他(非图像)代码路径,没有任何明显的错误。该代码有效吗?

在这两个代码路径中,您不需要 blob.SetMetadata()blob.SetProperties(),因为这些将在上传时完成。

[编辑] 另外,GetMimeFromFile 是做什么的?它是否从流中读取(因此可能将流位置保留在开头以外的位置)?

关于c# - Azure存储,无法正确上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10908830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com