gpt4 book ai didi

c# - 上传到 Azure 存储的文件已损坏

转载 作者:行者123 更新时间:2023-12-03 04:33:43 27 4
gpt4 key购买 nike

我试图允许用户通过 HTML5 页面将图像上传到我的网站,并尝试使用 Azure Blob 存储。我已经在后端创建了页面和 C# 代码,但图像在上传过程中似乎已损坏。我能够获取图像并将其传递到 Azure,它会创建 blob 并填充正确的大小,但是当下载图像然后打开时,我只会得到一堆垃圾(随机字母和符号)而不是图像。谁能帮我解决这个问题,我已经尝试解决这个问题一个多星期了。

[HttpPost, ValidateAntiForgeryToken]
public ActionResult AddSection(AddEditSectionVM model, HttpPostedFileBase LogoFile)
{
try
{
using (var db = new SectionContext())
{
if (db.Sections.Where(s => s.Route == model.Route).Count() > 0)
{
ModelState.AddModelError("Section Route Used", "A section with the same route already exists.");
}

if (LogoFile != null)
{
if (FileHelper.IsValidImage(LogoFile))
{
ModelState.AddModelError("Invalid File Type.", "Images must be JPG, GIF, or PNG files.");
}
}

if (ModelState.IsValid)
{
if (LogoFile != null)
{
string FileName = "Images/" + model.Route + "/Core/Logo" + Path.GetExtension(LogoFile.FileName).ToLower();
model.LogoFileID = FileHelper.UploadFile(FileName, "site", LogoFile);
}

var NewSection = new Section()
{
LogoFileID = model.LogoFileID,
Route = model.Route,
Title = model.Title,
Type = model.Type,
Synopsis = model.Synopsis
};

db.Sections.Add(NewSection);
db.SaveChanges();
ViewBag.Results = "New section added.";
}

return View(model);
}
}
catch (Exception ex)
{
ErrorSignal.FromCurrentContext().Raise(ex);
ViewBag.Results = "Error saving section, please try again later.";
return View(model);
}
}

public static int UploadFile(string FileName, string Container, HttpPostedFileBase UploadedFile, int ExistingFileID = 0)
{
var StorageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);
var BlobClient = StorageAccount.CreateCloudBlobClient();
var StorageContainer = BlobClient.GetContainerReference(Container);
var BlobBlock = StorageContainer.GetBlockBlobReference(FileName);
BlobBlock.Properties.ContentType = UploadedFile.ContentType;

using (UploadedFile.InputStream)
{
BlobBlock.UploadFromStream(UploadedFile.InputStream);
}

using (var db = new SectionContext())
{
var NewFile = new DAL.File();

if (ExistingFileID > 0)
{
NewFile = db.Files.Find(ExistingFileID);
NewFile.Name = FileName;
NewFile.ContentType = UploadedFile.ContentType;
db.Entry(NewFile).State = EntityState.Modified;
db.SaveChanges();
return ExistingFileID;
}
else
{
NewFile.Name = FileName;
NewFile.ContentType = UploadedFile.ContentType;
db.Files.Add(NewFile);
db.SaveChanges();
return NewFile.ID;
}

}
}

从此代码上传的损坏图像的链接:https://gisstore01.blob.core.windows.net/ffinfofiles/ChocoboontheJob/Core/Logo.jpg

最佳答案

这就是我用来接受 Logo 图像的...

var memoryStream = new MemoryStream();

logoFile.InputStream.CopyTo(memoryStream); //logoFile is HttpPostedFileBase
memoryStream.Position = 0;
await transferService.Upload(memoryStream, fileName, "merchant-app-data", true);

memoryStream.Close();
memoryStream.Dispose();

这里是实际的上传方法

public async Task<string> Upload(MemoryStream stream, string fileName, string folder, bool overwrite = false)
{
var blobClient = _storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(folder);
await container.CreateIfNotExistsAsync();

var blockBlob = container.GetBlockBlobReference(fileName);

blockBlob.UploadFromStream(stream);

return blockBlob.StorageUri.PrimaryUri.AbsolutePath;
}

关于c# - 上传到 Azure 存储的文件已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31733382/

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