Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
936 views
in Technique[技术] by (71.8m points)

sharepoint online - Add JSPDF File as a list item attachment

We are trying to save an attachment to multiple list items created concurrently. The PDF files are created dynamically using jspdf and stored against the list items

Below are the functions that create list item attachment by calling a common function named addItem().

Functions that call addItem()

function p1()
{
   CreateItem(fromEmail,distEmail,ccEmail,subject,body).then(function(resp){
                        
   console.log(resp);
   var Lid = resp.d.Id; // gets the list item ID
   var PDFStream = PDFArray; // Global Variable to get the jsPDF File
   var Fname = DFileName; // Global Variable to get the name of the attachment
   addItem(Lid,PDFStream,Fname);
  });
}

function p2()
{
   CreateItem(fromEmail,distEmail,ccEmail,subject,body).then(function(resp){
                        
   console.log(resp);
   var Lid = resp.d.Id; // gets the list item ID
   var PDFStream = PDFArray; // Global Variable to get the jsPDF File
   var Fname = DFileName; // Global Variable to get the name of the attachment
   addItem(Lid,PDFStream,Fname);
  });
}

Function to add attachment

function addItem(Lid,PDFFile,Fname) {
                        
            var File = PDFFile;
            var deferred = $.Deferred();           

                $.ajax({
                    url: _spPageContextInfo.webAbsoluteUrl +
                     "/_api/web/lists/getbytitle('ExtEmailsList')/items(" + Lid + ")/AttachmentFiles/add(FileName='"+Fname+"')",
                    type: 'POST',
                    data: File,
                    processData: false,
                    headers: {
                        "Accept": "application/json; odata=verbose",
                        "content-type": "application/json; odata=verbose",
                        "X-RequestDigest": $('#__REQUESTDIGEST').val(),
                        "content-length": File.length
                    },
                    success: function (data) {          
                    deferred.resolve(data);
                    },
        
                    error: function (err) {
                    deferred.reject(err);
                    }
               }); // ajax | POST      
        return deferred.promise();    
      };

The above function is called multiple times , However the function intermittently creates the attachment and the other times it fails and the attachment column is blank. Say function 1 calls addItem , it creates the attachment however the call from function 2 fails and vice versa.

I tried debugging , However there are no errors. Would appreciate if anyone could let us know how we can get this to work with multiple functions calling addItem().

Thanks in advance

question from:https://stackoverflow.com/questions/65880067/add-jspdf-file-as-a-list-item-attachment

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...