使用summernote将图像上传到Azure blob

时间:2016-08-30 13:35:34

标签: azure file-upload summernote

public class StorageService
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=removed for this post");

        public async Task Upload(string id, Stream data)
        {
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            // Retrieve a reference to a container.
            CloudBlobContainer container = blobClient.GetContainerReference("images");

            await container.CreateIfNotExistsAsync();

            container.SetPermissions(
                new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });

            // Retrieve reference to a blob named "myblob".
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(id);

            await blockBlob.UploadFromStreamAsync(data, data.Length);
        }

        public async Task UploadBlogPhoto(string id, Stream data)
        {
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            // Retrieve a reference to a container.
            CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

            await container.CreateIfNotExistsAsync();

            container.SetPermissions(
                new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });

            // Retrieve reference to a blob named "myblob".
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(id);

            await blockBlob.UploadFromStreamAsync(data, data.Length);
        }
    }

所以我有StorageServices类,我使用第一种方法Upload,上传用户'个人资料。

这是标记:

using (Html.BeginForm("UploadPhoto", "Manage", FormMethod.Post, new { enctype = "multipart/form-data" }))
            {
                <div class="browseimg">
                    <input type="file" class="display-none" name="file" id="files" onchange="this.form.submit()" />
                </div>
            }
            <button class="btn btn-primary width-100p main-bg round-border-bot" id="falseFiles">
                Upload billede
            </button>

enter image description here

这是用于上传照片的夏令营按钮。帖子上传到数据库。但是,有没有办法将文本上传到数据库并将图像上传到Azure blob?我是否需要首先上传图片并将azure blob的URL添加到summernote中异步进行,是否可以完成?

1 个答案:

答案 0 :(得分:1)

正如Gaurav所说,您可以在上传功能中执行这两项操作。在我看来,我建议您在上传操作中执行这两项操作,以确保数据的一致性。在这里,我为您提供了一个代码示例,以便更好地理解它。

<强> ManageController.cs

/// <summary>
/// Upload photo with description
/// </summary>
/// <param name="imageNote">description of the photo</param>
/// <returns></returns>
public async Task<JsonResult> UploadPhoto(string imageNote)
{
    string operationResult = string.Empty;
    string uploadedImageUrl = string.Empty;
    uploadedImageUrl = await UploadImageToBlob();
    //make sure the image is uploaded successfully to Azure Blob
    if (!string.IsNullOrEmpty(uploadedImageUrl))
    {
        //insert the image(blob) Url and imageNote to your database
        operationResult = "Operation is successful!";
    }
    else
        operationResult = "Image upload failed, please check and submit again!";

    return Json(new
    {
        Message = operationResult
    });
}

/// <summary>
/// Upload photo to Azure Blob Storage
/// </summary>
/// <returns>the new Blob(photo) Url</returns>
private async Task<string> UploadImageToBlob()
{
    string uploadedImageUrl = string.Empty;
    try
    {
        var files = Request.Files;
        if (files != null && files.Count > 0)
        {
            var file = files[0];
            string blobName = Path.GetFileName(file.FileName);

            #region upload image to Azure Blob and retrieve the Blob Url
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("brucechenStorage"));
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            // Retrieve a reference to a container.
            CloudBlobContainer container = blobClient.GetContainerReference("images");
            await container.CreateIfNotExistsAsync();
            // Retrieve reference to a blob named "blobName".
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
            await blockBlob.UploadFromStreamAsync(file.InputStream);
            uploadedImageUrl = blockBlob.Uri.ToString();
            #endregion
        }
    }
    catch (Exception e)
    {
        //TODO:log
    }
    return uploadedImageUrl;
}