如何在不使用MultipartFormDataStreamProvider保存的情况下直接获取图像?

时间:2017-11-11 01:59:15

标签: c# azure multipartform-data

我有一个Web API,它接收多个图像文件。目前,我将图像保存在本地磁盘中,然后对其执行方法。然后将其本地副本上载到Azure存储。我希望能做的是 -

1)直接获取图像(如果可能,然后执行方法)  2)将pdf直接保存到Azure存储。

我的代码看起来像这样。

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");
    }

现在,当我将其保存在本地磁盘中并上传到Azure时,此功能正常。但是,在将功能发布到Azure门户后,将其保存在本地磁盘中无法正常工作。

所以我想获取图像,然后将它们添加到PDF中并上传到Azure存储。我怎样才能做到这一点?

0 个答案:

没有答案