Search code examples
c#asp.net-mvcazureazure-blob-storageimageurl

Upload image to Azure Blob Storage from URL


Is it possible to have the CloudBlockBlob.UploadFromStreamAsync accept URL for images? I am trying to use the LinkedIn basicprofile api to retrieve the picture URL of the user (already have the URL) and I am trying to maybe download then upload the picture to the Azure Blob, like they selected a picture from their computer.

This is how it looks like now:

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>

The method in the controller:

public async Task<ActionResult> UploadPhoto(HttpPostedFileBase file)
{

    if (file != null && file.ContentLength > 0)
    {
        var fileExt = Path.GetExtension(file.FileName);
        if (fileExt.ToLower().EndsWith(".png") 
             || fileExt.ToLower().EndsWith(".jpg") 
             || fileExt.ToLower().EndsWith(".gif"))
        {
            var user = await GetCurrentUserAsync()
            await service.Upload(user.Id, file.InputStream);
        }
   }
    return RedirectToAction("Index")
}

Solution

  • Looks like all I needed was a simple

     WebClient wc = new WebClient();
     MemoryStream stream = new MemoryStream(wc.DownloadData("https://media.licdn.com/mpr/..."));
    

    and then

    await service.Upload(user.Id, stream);