Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Function couldnt find document openXML

I am new in Azure Functions and i am try to discover some features of it.

So i created function blob trigger, added references of OpenXML sdk in project.json

"frameworks": {
"net46":{
  "dependencies": {
    "WindowsAzure.Storage": "7.0.0" ,
    "Open-XML-SDK" : "2.7.2",
    "DocumentFormat.OpenXml" : "2.8.1"

And in run.csx i added next code

using System;
using System.IO;
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using DocumentFormat.OpenXml.Presentation;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;

public static  void Run (Stream myBlob, string name, TraceWriter log) 
{
            // *While we upload a blob, function start to execute*
            StreamReader reader = new StreamReader(myBlob);
            string S = reader.ReadToEnd();
            int numberofSlides = CountSlides(S);
            log.Info($"Number of slides = {numberofSlides}");

}

 // *here, using XML sdk we open our presentation without using Office*

 public static int CountSlides(string presentationFile)
        {
 using (PresentationDocument presentationDocument =PresentationDocument.Open(presentationFile, false))
            {
                return CountSlides(presentationDocument);
            }
        }

        public static int CountSlides(PresentationDocument presentationDocument)
        {
            if (presentationDocument == null)
            {
                throw new ArgumentNullException("presentationDocument");
            }

            int slidesCount = 0;

            PresentationPart presentationPart = presentationDocument.PresentationPart;
            if (presentationPart != null)
            {
                slidesCount = presentationPart.SlideParts.Count();
            }

            return slidesCount;
        }

The problem is while i upload a .pptx , i have error in log

Exception while executing function: Functions.BlobTriggerCSharp1. mscorlib: Exception has been thrown by the target of an invocation. DocumentFormat.OpenXml: Could not find document.

I upload a file .pptx using Microsoft Azure Storage Explorer and I can not understand why i have an error.

like image 619
Artem Averin Avatar asked Aug 29 '26 16:08

Artem Averin


1 Answers

You are using the following method:

public static PresentationDocument Open(
    string path,
    bool isEditable
)

The first parameter it accepts is path but you pass the file contents in there. It tries to find this weird file path and throws exception.

Try using the Stream overload.

like image 156
Mikhail Shilkov Avatar answered Aug 31 '26 06:08

Mikhail Shilkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!