I am trying to allow users to upload images to my web site via an HTML5 page and am trying to use Azure Blob storage. I have created the page and the C# code on the backend but it seems that the images are getting corrupted during the upload process. I am able to take the images and pass it to Azure and it creates the blob and fills it of the right size but when the images is downloaded and then open I just get a bunch of garbage (random letters and symbols) instead of the image. Can anyone help me figure this out, been trying to fix this for over a week now.
[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;
}
}
}
Link to broken image uploaded from this code: https://gisstore01.blob.core.windows.net/ffinfofiles/ChocoboontheJob/Core/Logo.jpg