如何使用ImageResizer处理图像并将其上传到Azure存储?

时间:2018-07-02 12:51:40

标签: asp.net-mvc azure kendo-ui imageresizer

我在MVC应用程序中使用ImageResizer nuget包来调整上传图像的大小并将其转换为JPG。然后,该图像应上传到我的Azure存储帐户。尝试上传以下图片文件时收到错误消息:

  

访问路径'C:\ Program Files(x86)\ IIS   Express \ Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.jpg'   被拒绝

因此,我将某些错误连接起来,但是我想确保自己确实在正确执行此操作。这是我的代码:

CarController

public ActionResult CarImageUpload(HttpPostedFileBase CarImage, string id)
{
    //Connect to Azure
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("cardata_AzureStorageConnectionString"));

        //Process the Image
        try
        {
            //Create Blob Client
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            //Retrieve a reference to a container
            CloudBlobContainer blobContainer = blobClient.GetContainerReference("car-" + id);

            //Process Image
            ImageJob image = new ImageJob(CarImage, blobContainer + ".<ext>",
                new Instructions("width=500&format=jpg;mode=max"));

            //Create the container if it doesn't already exist
            try
            {

                blobContainer.CreateIfNotExists();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            try
            {                  
                image.Build();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            try
            {
                //Upload to Azure
                CloudBlockBlob blob = blobContainer.GetBlockBlobReference(CarImage.FileName);
                blob.UploadFromStream(CarImage.InputStream);
                CarImage.InputStream.Close();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }           
        return RedirectToAction("Index");
    }

查看

@(Html.Kendo().Upload()
.Name("CarImage")
.Multiple(false)
.Validation(validation => validation.AllowedExtensions(new string[] { ".gif", ".jpg", ".png" }))
.Events(e => e
    .Upload("onFileSync")
    .Remove("onFileSync"))
    .Async(a => a
        .Save("CarImageUpload", "Car")
        .Remove("CarImageRemove", "Car")
        .AutoUpload(true)))

我认为我进行此连接的方式是在本地磁盘上寻找我的Azure存储,但我不确定如何确保它正在寻找Azure。谁能帮我解决这个问题?

1 个答案:

答案 0 :(得分:0)

问题与上传到Azure存储无关。

请参阅此声明

 ImageJob image = new ImageJob(CarImage, blobContainer + ".<ext>",
            new Instructions("width=500&format=jpg;mode=max"));

ImageJob的第二个参数是创建调整大小的图像的目标。

因此,方法是在IIS工作目录blobContainer + ".<ext>"上创建名为Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.jpg的映像,即C:\Program Files (x86)\IIS Express

要访问C:\Program Files (x86),我们需要以管理员身份运行IDE,否则将收到错误Access to the path ... is denied

您可以将调整大小后的图像写入MemoryStream,而不必以管理员身份运行。

var memoryStream = new MemoryStream();
ImageJob image = new ImageJob(CarImage, memoryStream,
            new Instructions("width=500&format=jpg;mode=max"));

如果我对它的理解正确,那么您实际上是想将此调整大小的图像上载到Azure存储。只需将blob.UploadFromStream(CarImage.InputStream);更改为

memoryStream.Position = 0;
blob.UploadFromStream(memoryStream);