'i want to upload a file from frontend to azure blob storage using restapi without using sdk using asp.net mvc

iam trying to upload any files to azure blob storage without using sdk using restapi in asp.net mvc

i got error if the content type image mean it will uploaded image only if i change content type file mean it will upload a file type but i need to upload every file to azure blob

here is my complete code.. please can anyone done this share the complete code

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace MVCuploadfile.Controllers
{
    public class HomeController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(HttpPostedFileBase file)
        {
            byte[] imageBytes = null;
            BinaryReader reader = new BinaryReader(file.InputStream);
            imageBytes = reader.ReadBytes((int)file.ContentLength);

            if (file.ContentLength > 0)
            {
                string _FileName = Path.GetFileName(file.FileName);
                //string _path = Path.Combine(Server.MapPath("~/UploadedFiles"), _FileName);
                //file.SaveAs(_path);
            }

            string storageKey = "account-key";
            string storageAccount = "account-name";
            string containerName = "fileuploads";
            string blobName = Path.GetFileName(file.FileName);

            string method = "PUT";
            //int sampleContent = file.ContentLength;
            //byte[] fileBytes = Convert.ToBase64String(file.ContentLength)
            byte[] fileBytes = imageBytes;
            ///int contentLength = Encoding.GetBytes(fileBytes);
            // int contentLength = sampleContent;

            string requestUri = "https://gallerywebphp.blob.core.windows.net/fileuploads/" + blobName;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);

            string now = DateTime.UtcNow.ToString("R");

            request.Method = method;
            request.ContentType = "image/png";
            request.ContentLength = file.ContentLength;

            request.Headers.Add("x-ms-version", "2017-04-17");
            request.Headers.Add("x-ms-date", now);
            request.Headers.Add("x-ms-blob-type", "BlockBlob");
            request.Headers.Add("Authorization", AuthorizationHeader2(method, now, request, storageAccount, storageKey, containerName, blobName));

            using (Stream requestStream = request.GetRequestStream())
            {
                //requestStream.Write(Encoding.UTF8.GetBytes(sampleContent), 0, contentLength);
               requestStream.Write(fileBytes, 0, file.ContentLength);
            }

            //using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
            //{
            //    Console.WriteLine(resp.StatusCode.ToString());
            //    Console.ReadKey();
            //}
            return View();


        }

        public static string AuthorizationHeader2(string method, string now, HttpWebRequest request, string storageAccount,
            string storageKey, string containerName, string blobName)
        {

            string headerResource = $"x-ms-blob-type:BlockBlob\nx-ms-date:" + now + "\nx-ms-version:2017-04-17";
            string urlResource = "/gallerywebphp/fileuploads/" + blobName;
            string stringToSign = method + "\n\n\n" + request.ContentLength +
                "\n\n" + request.ContentType + "\n\n\n\n\n\n\n" + headerResource + "\n" + urlResource;


            HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(storageKey));
            string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));

            String AuthorizationHeader = String.Format("{0} {1}:{2}", "SharedKey", storageAccount, signature);
            return AuthorizationHeader;
        }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source