上传到Azure存储文件损坏 [英] Files uploaded to Azure Storage are corrupted

查看:136
本文介绍了上传到Azure存储文件损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让用户通过HTML5页面上传图片到我的网站,我试图使用Azure的BLOB存储。我创建了页面,并在后端的C#code,但似乎图像在上传过程中得到破坏。我能够拍摄的图像,并把它传递给Azure和它创建的BLOB和填充大小合适的,但是当图像被下载,然后打开我只是得到一堆垃圾(随机字母和符号),而不是图像。谁能帮我想出解决办法,一直在努力解决这一问题超过现在一个星期。

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;
        }

    }
}

链接打破这一code上传的图片:的https://gisstore01.blob.core.windows.net/ffinfofiles/ChocoboontheJob/Core/Logo.jpg

推荐答案

您code看起来不错。我没有使用尽可能多的code尽可能的看到,如果有什么需要注意的快速原型,但一切似乎很好地工作。

Your code looks fine. I did a quick prototype using as much of your code as possible to see if there was anything to watch out for, but everything seems to work fine.

我的猜测是,这个问题是不是在上传本身,而是对访问容器。默认情况下的Blob容器不会允许公众匿名访问。

My guess is the problem is not on the upload itself but on the access to the container. By default Blob containers will not allow public anonymous access.

为了改变这种状况,更简单的方法是去Visual Studio中,通过服务器资源管理器连接到您的Azure实例,然后找到你的Blob容器,并点击它。在属性窗口中,你应该可以看到公共读取权限和三个选项的条目:关,集装箱或BLOB

In order to change that, the easier way is to go on Visual Studio, connect to your Azure instance via Server Explorer then find your Blob container and click on it. On the properties window you should see an entry for Public Read Access and three options: Off, Container or Blob.

如果是关,那么这就是你的问题!选择Container或斑点根据您的需求,虽然它的安全选择斑点。所不同的是,随着斑点匿名用户只能访问到您的blob数据,但对斑点水平不能访问任何东西,所以举例来说,他们不能枚举所有的Blob容器中,但他们可以通过BLOB下载图片链接。

If it's Off, then that's your problem! Choose either Container or Blob depending on your needs though it's safer to choose Blob. The difference being that with Blob anonymous users only have access to the data in your blobs but cannot access anything on the Blob level, so for instance, they can't enumerate all blobs in the container, but they can download an image via the blob link.

如果您想了解更多关于容器权限这里的MSDN链接吧:的https://azure.microsoft.com/en-us/documentation/articles/storage-manage-access-to-resources/

If you want to read more about container permissions here's the MSDN link for it : https://azure.microsoft.com/en-us/documentation/articles/storage-manage-access-to-resources/

希望这有助于。如果没有,请告诉我们,我们会继续尝试。

Hope this helps. If not, let us know and we'll keep trying.

这篇关于上传到Azure存储文件损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆