sp-dev-docs icon indicating copy to clipboard operation
sp-dev-docs copied to clipboard

SPFx Custom Export Blob 404

Open Cyan005 opened this issue 7 years ago • 13 comments

Category

  • [ ] Question
  • [ ] Typo
  • [x] Bug
  • [ ] Additional article idea

Expected or Desired Behavior

Within a SPFx web part, when a user clicks a button, an export of data occurs. A simple CSV export of SharePoint list data as well as an export of a PowerPoint file.

Observed Behavior

Currently have multiple SPFx web parts deployed using a site collection app catalog. These web parts have functioned as expected for over a year now, but all of a sudden an issue is appearing during a data export operation.

Two unique exports from SPFx web parts:

  1. Using xslx (SheetJS) to perform an export to CSV of SharePoint list data.
  2. Export to a PowerPoint file using PptxGenJS.

When an export operation is executed, the SharePoint site is calling a URL to blob storage (which it may have always done in the background), but now it's presenting a 404. The GUID is unique for each export.

Ex. https:///blob:/2096e5e9-2b06-4520-95e8-09c03e15a404

The user is redirected to the URL and the page displays 404 NOT FOUND.

The only thing I can come up with is there may have been a change on the back-end with how Microsoft handles these requests in the last week?

Edit: Just to add, everything works exactly as expected from the workbench. Performs the correct file download. At this point all of the data is stored within the web part using React. It's just simply taking the current state and exporting based on that data. The workbench never calls this blob URL at all to properly export. Not sure why it's being intercepted on the server all of a sudden.

Steps to Reproduce

Perform a custom export of data using a SPFx web part.

Cyan005 avatar Nov 27 '19 17:11 Cyan005

Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.

msft-github-bot avatar Nov 27 '19 17:11 msft-github-bot

Providing an update: the download for both the XLSX and PPT files appear to work as expected within Internet Explorer 11, but not in either Chrome or Firefox. Investigating further...

Cyan005 avatar Dec 03 '19 17:12 Cyan005

Developed a bare-bones web part that all it does is exports an Excel file generated with SheetJS. Same findings, works as expected within Internet Explorer only. Also again works as expected within the workbench using both Chrome and Firefox. But when deployed to the tenant funny things start happening. Confirmed consistent results from two tenants, one with a site collection app catalog, another with a global.

Just some other screen shots to maybe help address what's going on. When the client-side generation and export is performed through either Chrome or Firefox, here are a few screen shots of what's being displayed - again nothing as changed on the code side and then all of a sudden over the last 2 weeks this began happening:

SPFX

SPFX1

Cyan005 avatar Dec 05 '19 15:12 Cyan005

Upon doing more research and diving deeper into the dependencies, it appears that URL.createObjectURL(blob) is being used for both Chrome and Firefox. In Internet Explorer the code appears to be using the msSaveBlob (which is why within IE it appears to work as expected). I stumbled on a previous issue which appears to be the exact opposite of what I'm experiencing. In that case it worked in Chrome and FF, but not IE. The issue I'm experiencing today may be down the same line and it appears the resolution was implemented by Microsoft with no interaction from the developer:

#754

Cyan005 avatar Dec 05 '19 22:12 Cyan005

Final update... I've confirmed the export and web part work as expected when I create a classic page and add the SPFx web part to it. The classic page does not use the sp-pages-assembly as referenced earlier. The primary issue I'm experiencing appears to be related in a recent update to the following file within the tenant (or in general) and how it interacts with the URL.creatObjectURL() function.

https://spoprod-a.akamaihd.net/files/sp-client/sp-pages-assembly_en-us_6c1243ef07241a9f41fe54d8c7adbc98.js

Cyan005 avatar Dec 05 '19 22:12 Cyan005

Can you provide a link to a public GH repro project to repro?

andrewconnell avatar Dec 06 '19 16:12 andrewconnell

A repository for the basic export test can be found here: https://github.com/Cyan005/SPFxExportSample

Add the web part to a modern page and attempt to export. This cannot be tested in workbench as it will work as expected.

Thanks!

Cyan005 avatar Dec 07 '19 01:12 Cyan005

I see the same thing with Chrome, FF & CrEdge (can't test IE... on MacOS), but I can't tell what's causing it. It's failing when it tries to stream the file back... may be related to the partial page navigation in SPO... but can't tell if it's an issue with the library you're using.

One thing I'd do first is to upgrade to the SPFx... you're on 1.8... latest is 1.9.1. That ensures you're using the latest on the page. Second, you should update the code to more safely get a reference to the button (getElementById is unsafe... what if there are multiple web parts on the page?).

I changed it to

<span class="exportButton ${ styles.label }">Export to Excel</span>
...
let clickEvent= this.domElement.getElementsByClassName("exportButton")[0];
clickEvent.addEventListener("click", (e: Event) => this.exportToExcel(e));

... and updated method reference, and to ensure the navigate event on the <a> tag doesn't fully fire...

  private exportToExcel = (e: Event): void => {
    e.preventDefault();
    ..
  }

These won't fix the problem you're having... ping'd a few folks to see if they have ideas & will follow up if I find anything...

andrewconnell avatar Dec 07 '19 11:12 andrewconnell

Thanks @andrewconnell

To add, I did previously perform a full upgrade to SPFx 1.9.1 on the production solution as well as all dependency updates to their latest version with the same result.

The actual solution also uses React, but since that's not where the problem is, the sample solution provided was just the most straight-forward way to demonstrate the issue.

The web parts deployed to the production tenant have been out there for almost a year with no code change regarding the export, then out of the blue this started happening.

Thanks again!

Cyan005 avatar Dec 07 '19 12:12 Cyan005

@Cyan005 @andrewconnell

Recent SPFx changes relating to how hyperlinks are handled is causing this issue (see docs).

In order to have blobs resume downloading like they used to, the new data-interception="off" property is required.

let blob = new Blob([content], { type: 'text/csv;charset=utf-8;' })
let url = URL.createObjectURL(blob)
let link = document.createElement('a')
link.href = url
link.download = 'some-sheet.xlsx'
link.style.visibility = 'hidden'
link.dataset.interception = 'off'
document.body.appendChild(link)
link.click()

gitbrent avatar Dec 09 '19 21:12 gitbrent

@gitbrent is correct regarding the data-interception point, and that is actually the issue with the library, however, to give you a more complete answer, if you look at the documentation for SheetJS you'll see that what it's doing when you save a file using the library is creating that anchor tag and then triggering a click event on it.

URL browser API creates an object URL for the file, which the library uses by creating a link and forcing a click. It is supported in modern browsers.

Given the issue with the data-intercept then not being present I suspect that even though data-intercept has been around for at least a couple years at this point something else has changed with regard to file downloads off anchor tags, although that's just a guess. Regardless, relying on that library's writeFile method is probably not going to be stable. As I see it you have a couple of choices:

  1. Write your own save method, similar to @gitbrent suggested.
  2. Use the Excel extension of the graph to save the file to the users OneDrive or another SharePoint library so that the user can work on it from there, either because it's being synced to their desktop or so they can work on it in a shared location. For more information on the working with Excel files using Graph check out the documentation here, or if you want I have a public sample shared here that reads data from a sharepoint library and writes it into an Excel sheet.

juliemturner avatar Dec 10 '19 14:12 juliemturner

Thanks for the responses everyone! So it pretty much sounds like as long as the 3rd party libraries have the ability to save the blob, we can take it and properly export it by turning off the interception.

My final thought would be that there's a good chance I won't be the only one to run into this moving forward and without extensive troubleshooting and community support, would never have been able to really hurdle it. Especially if the data interception has been activated for quite a while with the web parts functioning as expected.

Thanks @gitbrent and team!

Cyan005 avatar Dec 10 '19 16:12 Cyan005

Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues

msft-github-bot avatar Jan 23 '20 00:01 msft-github-bot