0

I'm developing rest web api that uploads images to my azure blob, I used one of the online tutorial and got this code

 public class DocumentsController : ApiController
{
    private const string CONTAINER = "documents";

    // POST api/<controller>
    public async Task<HttpResponseMessage> Post()
    {
        var context = new StorageContext();

        // Check if the request contains multipart/form-data.
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        // Get and create the container
        var blobContainer = context.BlobClient.GetContainerReference(CONTAINER);
        blobContainer.CreateIfNotExists();

        string root = HttpContext.Current.Server.MapPath("~/App_Data");
        var provider = new MultipartFormDataStreamProvider(root);

        try
        {
            // Read the form data and return an async task.
            await Request.Content.ReadAsMultipartAsync(provider);

            // This illustrates how to get the file names for uploaded files.
            foreach (var fileData in provider.FileData)
            {
                var filename = fileData.LocalFileName;
                var blob = blobContainer.GetBlockBlobReference(filename);

                using (var filestream = File.OpenRead(fileData.LocalFileName))
                {
                    blob.UploadFromStream(filestream);
                }
                File.Delete(fileData.LocalFileName);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }
        catch (System.Exception e)
        {
            return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
        }
    }

it is uploading the image in my account blob container but when i open azure storage manager and access the container i get wrong format as below picture describes

enter image description here

can u see the content-type ? I'm not able open this path in explorer any help would be appreciated

ahmed
  • 45
  • 1
  • 10

1 Answers1

0

Is your "filename" from code below have extension included when you debuging (like .jpg, .png)? For example "image.jpg"

var blob = blobContainer.GetBlockBlobReference(filename);

Also "fileData.LocalFileName" from code below need to have file extension

using (var filestream = File.OpenRead(fileData.LocalFileName))

It doesn't have extension and for this reason you have such issue

Sergiy Divnych
  • 149
  • 2
  • 13
  • actually, it does't have, what should i change in order to let it work – ahmed May 18 '18 at 14:26
  • and why is the name is formatted in wrong way, I'm not getting the right name for the file – ahmed May 18 '18 at 14:27
  • @ahmed Name for ffile is generated by Azure and this name consist of "BodyPart" in your case + Guid. You should send to this method uri which includes file extension for example: `AvatarUri = GeneralConsts.ProfilePath + Guid.NewGuid() + GeneralConsts.Jpg;`' – Sergiy Divnych May 18 '18 at 14:33
  • @ahmed Better to create some helper in which you will upload file to Azure blob which will receive image from another method, with full file name and extension – Sergiy Divnych May 18 '18 at 14:36
  • sorry Divnch but still not getting it – ahmed May 19 '18 at 00:31
  • now its working fine but the problem is that when i store two pics with the same name, one of them gets replaced – ahmed May 19 '18 at 14:09
  • @ahmed When you uploading picture the best way to set some Guid inside the file name. Example you can leave your file name add Guid and in the end add file extension. In that case you wouldn't have such issue – Sergiy Divnych May 20 '18 at 07:11
  • @ahmed If it has helped you (eventually) please don't forget to mark it as the answer, thanks! – Sergiy Divnych May 21 '18 at 14:03