<appSettings>
<add key="StorageAccountName" value="YOURAZURECHINAACCOUNTNAME" />
<add key="StorageAccountKey" value="YOURAZURECHINAACCOUNTKEY" />
</appSettings>
The below is controller code
I am using service here the code are as below:-
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
namespace BlobStorageDemo
{
public class ImageService
{
public async Task<string> UploadImageAsync(HttpPostedFileBase imageToUpload)
{
string account = CloudConfigurationManager.GetSetting("StorageAccountName");
string key = CloudConfigurationManager.GetSetting("StorageAccountKey");
string imageFullPath = null;
if (imageToUpload == null || imageToUpload.ContentLength==0)
{
return null;
}
try
{
StorageCredentials credn = new StorageCredentials(account, key);
CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(credn, new Uri("https://deice2staging.blob.core.chinacloudapi.cn "),
new Uri(" https://deice2staging.blob.core.chinacloudapi.cn/ "),
new Uri("https://deice2staging.blob.core.chinacloudapi.cn/ "), null);
//CloudStorageAccount cloudStorageAccount = ConnectionString.GetConnectionString();
CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("blobstor1");
if(await cloudBlobContainer.CreateIfNotExistsAsync())
{
await cloudBlobContainer.SetPermissionsAsync(
new BlobContainerPermissions {
PublicAccess = BlobContainerPublicAccessType.Blob
}
);
}
string imageName = Guid.NewGuid().ToString() + "-" + Path.GetExtension(imageToUpload.FileName);
CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(imageName);
cloudBlockBlob.Properties.ContentType = imageToUpload.ContentType;
await cloudBlockBlob.UploadFromStreamAsync(imageToUpload.InputStream);
imageFullPath = cloudBlockBlob.Uri.ToString();
}
catch (Exception ex)
{
}
return imageFullPath;
}
}
}
and my UI code are as below :-
@{
ViewBag.Title = "Upload";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Upload Image</h2>
<br />
@using (Html.BeginForm("Upload", "Image", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<fieldset class="form-horizontal">
<div class="form-group">
<label class="control-label col-md-2" for="Photo">Photo</label>
<div class="col-md-10">
<input type="file" name="photo" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Submit" class="btn" />
</div>
</div>
</fieldset>
}
Thank you .
Hope it is help you
No comments:
Post a Comment