0

I have a web API which receives multiple files which are images. At the moment I save the image in my local disk and then perform methods on it. And then upload the local copy of it to Azure storage. What I want to be able to do is -

1) Get the image directly (if possible and then perform methods) 2) Save the pdf directly to Azure storage.

My code looks like this.

public static class Imageupload
{
    [FunctionName("Imageupload")]
    public static async System.Threading.Tasks.Task<HttpResponseMessage> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "HttpTriggerCSharp/name/{name}")]HttpRequestMessage req, string name, TraceWriter log)
    {
        //Check if the request contains multipart/form-data.
        if (!req.Content.IsMimeMultipartContent())
        {
            return req.CreateResponse(HttpStatusCode.UnsupportedMediaType);
        }


    var storageConnectionString = "XXXXXXXXXXXXXXXXXXXXXXXXXX";


        var storageAccount = CloudStorageAccount.Parse(storageConnectionString);
        log.Info(storageConnectionString);
        var blobClient = storageAccount.CreateCloudBlobClient();

        // Retrieve a reference to a container.
        CloudBlobContainer container = blobClient.GetContainerReference("temporary-images");

        // Create the container if it doesn't already exist.
        container.CreateIfNotExists();

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

        //The root path where the content of MIME multipart body parts are written to

        var provider = new MultipartFormDataStreamProvider(@"C:\Users\Al\Desktop\Images\");

        var s = blockBlob.Uri.AbsoluteUri;

        await req.Content.ReadAsMultipartAsync();

        //Test function for aspose
        // Instantiate Document object
        var pdf = new Aspose.Pdf.Document();
        //Add a page to the document
        var pdfImageSection = pdf.Pages.Add();

        List<UploadedFileInfo> files = new List<UploadedFileInfo>();
        // This illustrates how to get the file names.
        foreach (MultipartFileData file in provider.FileData)
        {
            var fileInfo = new FileInfo(file.Headers.ContentDisposition.FileName.Trim('"'));
            files.Add(new UploadedFileInfo()
            {
               FileName = fileInfo.Name,
               ContentType = file.Headers.ContentType.MediaType,
                FileExtension = fileInfo.Extension,
                FileURL = file.LocalFileName
            });

            //Iterate through multiple images
                FileStream stream = new FileStream(file.LocalFileName, FileMode.Open);
                System.Drawing.Image img = new System.Drawing.Bitmap(stream);
                var image = new Aspose.Pdf.Image { ImageStream = stream };
                //Set appearance properties
                image.FixHeight = 300;
                image.FixWidth = 300;
                //Set margins for proper spacing and alignment
                image.Margin = new MarginInfo(5, 10, 5, 10);
                //Add the image to paragraphs of the document
                pdfImageSection.Paragraphs.Add(image);

        }

        //Save resultant document
        pdf.Save(@"C:\Users\Al\Desktop\Images\Image2Pdf_out.pdf");

        var ss = pdf.FileName;
        blockBlob.UploadFromFile(@"C:\Users\Al\Desktop\Images\Image2Pdf_out.pdf");


        return req.CreateResponse(HttpStatusCode.OK, files, "application /json");
        //return req.CreateResponse(HttpStatusCode.OK,"Its working","application/json");
    }

Now this works when I am saving this in my local disk and upload to Azure. But saving it in local disk wont work after I publish the function to Azure portal.

So I want to get the images, and then add them into a PDF and upload to Azure storage. How can I do this?

alan samuel
  • 415
  • 6
  • 28
  • This kind of file location does not exist on azure: `"C:\Users\Al\Desktop\Images\Image2Pdf_out.pdf"`. You should upload directly to azure storage instead – Khanh TO Nov 11 '17 at 02:26
  • @KhanhTO How can I save the images I am receiving directly to Azure storage when using MultipartformDataStreamProvider? – alan samuel Nov 11 '17 at 02:33
  • Please try this: https://stackoverflow.com/questions/27965568/webapi-file-uploading-without-writing-files-to-disk – Khanh TO Nov 11 '17 at 02:49
  • @KhanhTO any idea on how to do the same for PDF? (Not saving locally but uploading directly to azure storage? – alan samuel Nov 11 '17 at 03:11
  • it should be the same for pdf, we just save the contents as bytes in memory and then you just post the bytes to azure storage – Khanh TO Nov 11 '17 at 03:19
  • Possible duplicate of [Cant post to Azure storage? Invalid Path error](https://stackoverflow.com/questions/47225740/cant-post-to-azure-storage-invalid-path-error) – Bruce Chen Nov 13 '17 at 09:03

0 Answers0