PDF stored in a dataset is reporting Error 404

I have several PDF files stored in a dataset. in my code when it runs wixLocation.to( wix:document://v1/5700e7_582e0b79dad04bf88aa4f75b50f19a16.pdf/MyO_Abril26__20103_1.pdf) it just supposed to display the PDF file, but something happen last weeks and it stoped showing the files and insted I’m gettin a Error 404 Page not fould. This page is not available. I checked in the CMS, open the dataset and downloaded it, and was ok, also clicked on “Open File” and it showed the file.

This problem is happening in all the PDF files I have stored in the CMS.

Does somebody have a clue on what is happening? or a way to fix this problem?

Working in
e.g. Wix Studio Editor, Wix Editor, Dev Mode, CMS, etc.

Site link
wixLocation.to( wix:document://v1/5700e7_582e0b79dad04bf88aa4f75b50f19a16.pdf/MyO_Abril26__20103_1.pdf)

What I’ve tried so far
I have checked the link works… Opened the file directly from the CMS and checked the link in the code is correct.

Cleared the cashe, cookies.

Tryied in Google browser and Microdoft Edge

Tryed publishing the site

and nothing fix the issue

Checked the wix site if there is invoicing problem but everithing is ok. My site runs smoothly until I want to display a PDF

Extra context
Anything else that might help - edge cases, screenshots, etc.

This looks like the issue is the format being passed into wixLocation.to().

wix:document://... is Wix’s internal media reference format. It can be stored in the CMS and used by Wix elements, but it is not always safe to use directly as a browser URL. If Wix changed how document routing is handled recently, that would explain why it suddenly starts opening a 404 page.

The safer approach is to convert the CMS file reference into a real public Wix document URL before redirecting.

Thank you Dan!

Can you explain or can you tell me where can I find a document on how “to convert the CMS file reference into a real public Wix document URL before redirecting.”

The extrage thing is that I’ve been using this aproach for more tan 4 years, and abot 2 months ago, the proble started, it was for about 3 or 4 days and then it started working back again. Sudenly, last week this beheavor happend again

Thanks in advance

something like this - need to adjust to suit your dataset, button and pdf field key

havnt tested

import wixLocation from 'wix-location';

function getDocumentUrl(fileRef) {
    if (!fileRef) {
        return null;
    }

    // Converts:
    // wix:document://v1/5700e7_xxxxx.pdf/FileName.pdf
    // into:
    // https://static.wixstatic.com/ugd/5700e7_xxxxx.pdf
    const match = fileRef.match(/wix:document:\/\/v1\/([^/]+)/);

    if (!match || !match[1]) {
        return fileRef;
    }

    return `https://static.wixstatic.com/ugd/${match[1]}`;
}

$w.onReady(() => {
    $w('#dataset1').onReady(() => {
        const item = $w('#dataset1').getCurrentItem();

        $w('#button1').onClick(() => {
            const fileRef = item.pdfFieldKey;
            const documentUrl = getDocumentUrl(fileRef);

            if (documentUrl) {
                wixLocation.to(documentUrl);
            }
        });
    });
});

Hi Dan!

Thank you for your help

I have tested your sugestion and this is what I get

The original file name is

wix:document://v1/8028dd_d529bc771d01452796b7d32c6117be72.pdf/MyO_Abril26__60224_1_1.pdf

and after the match I get

documentUrl = “https://static.wixstatic.com/ugd/8028dd_d529bc771d01452796b7d32c6117be72.pdf

when I try to run “wixLocation.to(documentUrl)” (click in the button) it doesn’t run, it does not displays the pdf, it doesn’t happens nothing

When I put it on browser URL address, it displays the pdf.

Waht you think should I do to display the pdf?

Thanks in advance!!! :slight_smile:

can you paste your code ? did you update the button id ?

maybe instead of wixLocation.to() use window.open()

$w(‘#TblReciboMyO’).onCellSelect((event) => {

//console.log("En SolatPlant1 - onRowSelect TblReciboMyO - event.cellData ", event.cellData);

//console.log("En SolatPlant1 - onRowSelect TblReciboMyO - $w(‘#TblReciboMyO’).rows[event.cellRowIndex].DocumentoMyO ", $w(‘#TblReciboMyO’).rows[event.cellRowIndex].DocumentoMyO);

    //console.log("En SolatPlant1 - onRowSelect TblReciboMyO -  event.cellColumnId ", event.cellColumnId);

    **if** (event.cellData == "Rep. MyO"){

        //wixLocation.to($w('#TblReciboMyO').rows\[event.cellRowIndex\].MyO); 

        //   Se cambio esto por que la referencia ce de wix:document://xxxxx.pdf no funciona 

        **const** fileRef = $w('#TblReciboMyO').rows\[event.cellRowIndex\].MyO;

        **const** documentUrl = getDocumentUrl(fileRef);



        **if** (documentUrl) {

            wixLocation.to(documentUrl);

            //window.open(documentUrl)

        }







        //console.log("En SolatPlant1 - onRowSelect TblReciboMyO -  $w('#TblReciboMyO').rows\[event.cellRowIndex\].MyO ", $w('#TblReciboMyO').rows\[event.cellRowIndex\].MyO);

        //console.log("En SolatPlant1 - onRowSelect TblReciboMyO -  event.cellRowIndex - MyO ", event.cellRowIndex);

        console.log("En SolatPlant1 - onRowSelect TblReciboMyO -  $w('#TblReciboMyO').rows\[event.cellRowIndex\].MyO ", $w('#TblReciboMyO').rows\[event.cellRowIndex\].MyO);

        console.log("En SolatPlant1 - onRowSelect TblReciboMyO -  documentUrl ", documentUrl);

    } 

    **if** (event.cellData == "Rec. CFE"){    

        //wixLocation.to($w('#TblReciboMyO').rows\[event.cellRowIndex\].ReciboCFE);

        //   Se cambio esto por que la referencia ce de wix:document://xxxxx.pdf no funciona 

        **const** fileRef = $w('#TblReciboMyO').rows\[event.cellRowIndex\].ReciboCFE;

        **const** documentUrl = getDocumentUrl(fileRef);



        **if** (documentUrl) {

            wixLocation.to(documentUrl);

        }





        //console.log("En SolatPlant1 - onRowSelect TblReciboMyO -  $w('#TblReciboMyO').rows\[event.cellRowIndex\].ReciboCFE ", $w('#TblReciboMyO').rows\[event.cellRowIndex\].ReciboCFE);

        //console.log("En SolatPlant1 - onRowSelect TblReciboMyO -  event.cellRowIndex - ReciboCFE", event.cellRowIndex);

        console.log("En SolatPlant1 - onRowSelect TblReciboMyO -  $w('#TblReciboMyO').rows\[event.cellRowIndex\].ReciboCFE ", $w('#TblReciboMyO').rows\[event.cellRowIndex\].ReciboCFE);

        console.log("En SolatPlant1 - onRowSelect TblReciboMyO -  documentUrl ", documentUrl);

    }



//console.log("En SolatPlant1 - onRowSelect TblMyO -  $w('#galDocTec').currentItem ", $w('#galDocTec').currentItem);

})

The link is inside a table cell, this is the console.log

En SolatPlant1 - onRowSelect TblReciboMyO - $w(‘#TblReciboMyO’).rows[event.cellRowIndex].MyO wix:document://v1/8028dd_d529bc771d01452796b7d32c6117be72.pdf/MyO_Abril26__60224_1_1.pdf

En SolatPlant1 - onRowSelect TblReciboMyO - documentUrl https://static.wixstatic.com/ugd/8028dd_d529bc771d01452796b7d32c6117be72.pdf

Your console log confirms the file reference is being converted correctly, so the issue is probably how the table click event is handled with wixLocation.to().

the browser may be blocking the popup because the click is coming from a table cell event.

This is the exact way it used to work, before I was getting the 404 Error.

When I clicked the cell, it opened another browser tab and showed the pdf

Hi Dan!

I found that wix in one dataset when uploads the pdf files it agregates an “udg/” prefix in the URL address

I coded the fix like this

function getDocumentUrl(fileRef) {

**if** (!fileRef) {

    **return** **null**;

}



// Converts:

// wix:document://v1/5700e7_xxxxx.pdf/FileName.pdf

// into:

// https://static.wixstatic.com/ugd/5700e7_xxxxx.pdf



**const** stUGD = /ugd\\//;

**let** match = "";



**if**(stUGD.test(fileRef)) {

    match = fileRef.match(/wix:document:\\/\\/v1\\/ugd\\/(\[^/\]+)/);

} **else** {

    match = fileRef.match(/wix:document:\\/\\/v1\\/(\[^/\]+)/);

}

**if** (!match || !match\[1\]) {

    **return** fileRef;

}



**return** \`https://static.wixstatic.com/ugd/${match\[1\]}\`;

}

Also I tested with a onRowSelect and onCellSelect but I got the same result … it doesn’t shows the pdf, is like clicking where there is not a link.

Following are the console.log rsults of running the getDocumentUrl(fileRef)

En SolatPlant1 - onCellSelect TblReciboMyO - $w(‘#TblReciboMyO’).rows[event.cellRowIndex].MyO wix:document://v1/8028dd_d529bc771d01452796b7d32c6117be72.pdf/MyO_Abril26__60224_1_1.pdf

En SolatPlant1 - onCellSelect TblReciboMyO - documentUrl https://static.wixstatic.com/ugd/8028dd_d529bc771d01452796b7d32c6117be72.pdf

En SolatPlant1 - onCellSelect TblReciboMyO - $w(‘#TblReciboMyO’).rows[event.cellRowIndex].ReciboCFE wix:document://v1/8028dd_107905203c5d4b548b80f5ad4a32000c.pdf/Recibo_343151010380Abril26.pdf

En SolatPlant1 - onCellSelect TblReciboMyO - documentUrl https://static.wixstatic.com/ugd/8028dd_107905203c5d4b548b80f5ad4a32000c.pdf

En SolatPlant1 - onRowSelect TblMyO - $w(‘#TblMyO’).rows[event.cellRowIndex].MyO wix:document://v1/ugd/0f7860_cd2d1d6b1755484f96e299f430097b58.pdf/CSI_DT_TOPBiHiKu7_CS7N-TB-AG_F46_685~720Wp_v1.8_EN.pdf

En SolatPlant1 - onRowSelect TblMyO - documentUrl https://static.wixstatic.com/ugd/0f7860_cd2d1d6b1755484f96e299f430097b58.pdf

and this is the code for - wixLocation.to(documentUrl) -

------------------------

onCellSelect

        **const** fileRef = $w('#TblReciboMyO').rows\[event.cellRowIndex\].MyO;

        **const** documentUrl = getDocumentUrl(fileRef);

        wixLocation.to(documentUrl);

--------------

onRowSelect

    **const** fileRef = $w('#TblMyO').rows\[event.rowIndex\].Documento;

    **const** documentUrl = getDocumentUrl(fileRef);

    wixLocation.to(documentUrl);

If I copy the documentUrl in the browser’s address URL filed, I get the pdf file supposed to get when the cell or the row is clicked.

I

Jumping in :slight_smile:

There’s one thing I’d probably change with the conversion to file URL - which is a more stable approach and will prevent the URL conversion breaking should Wix make changes to the way media is stored in the future.

Instead of manually parsing the wix:document:// reference, use themedia.getDocumentUrl() from the Wix SDK - Media

It’s as straightforward as:

import { media } from "@wix/sdk"

$w.onReady(() => {
	const url = media.getDocumentUrl(wixMediaIdentifier)
})

If possible, it would help with understanding the code, if it was formatted in a code block, which can be added from the toolbar at the top of the message composer :slight_smile:

Hi Noah,

Apprecite your advice, I made the changes as you recommended but I steel can not get the wixLocation() to show the file.

This is the file reference stored in the fileset

wix:document://v1/038db2_cca363a14e46419e90128a3a63a50e2b.pdf/Recibo_138111100154Abril26.pdf

and this is wat I get after running the media.getDocumentUrl

documentUrl = https://d945e594-8657-47e2-9cd9-e9033c3d8da0.usrfiles.com/ugd/038db2_cca363a14e46419e90128a3a63a50e2b.pdf

and the the code executes wixLocation.to(documentUrl);

But it just don’t shows the file, it doesn’t show any error it just stays like not being executed the wixLocation()

If I copy the link and paste into the google browser URL address it shows the file.

Any recommendation?

Can you share the full code (formatted as a code block) for the page?

function getDocumentUrl(fileRef) {
    if (!fileRef) {
        return null;
    }

    const fileUrl = media.getDocumentUrl(fileRef);
    return fileUrl.url;
}

$w('#TblMyO').onRowSelect((event) => {
    const fileRef = $w('#TblMyO').rows[event.rowIndex].Documento;
    const documentUrl = getDocumentUrl(fileRef);

    if (documentUrl) {
        wixLocation.to(documentUrl);
    }
});

Is this ok? I’m sorry don’t undesrtand what you mean by “formatted as a code block”

A few things I would recommend:

  • Add console logs:
    • Is the onRowSelect actually running?
    • What do you get inside the if for documentUrl?
  • Try a minimal setup
    • e.g. create a page with a minimum setup to isolate and make sure it’s working as you’d expect before adding to a large page of code

Thank you Noah,

  • Is the onRowSelect actually running? - Yes the console.log is inside the onRowSelect

  • What do you get inside the if for documentUrl? - This is the console.Log showing the documentUrl

En SolatPlant1 - onRowSelect TblMyO - $w(‘#TblMyO’).rows[event.cellRowIndex\].MyO wix:document://v1/ugd/0f7860_cd2d1d6b1755484f96e299f430097b58.pdf/CSI_DT_TOPBiHiKu7_CS7N-TB-AG_F46_685~720Wp_v1.8_EN.pdf

En SolatPlant1 - onRowSelect TblMyO - documentUrl https://static.wixstatic.com/ugd/0f7860_cd2d1d6b1755484f96e299f430097b58.pdf

This is the exact code without editing

$w(‘#TblMyO’).onRowSelect((event) => {

    **const** fileRef = $w('#TblMyO').rows\[event.rowIndex\].Documento;

    **const** documentUrl = getDocumentUrl(fileRef);



    console.log("En SolatPlant1 - onRowSelect TblMyO -  $w('#TblMyO').rows\[event.cellRowIndex\].MyO ", $w('#TblMyO').rows\[event.rowIndex\].Documento);

    console.log("En SolatPlant1 - onRowSelect TblMyO -  documentUrl ", documentUrl);



    wixLocation.to(documentUrl);

})

I can set up a minimum page, but it will take long, to populate the table, has a complex process where it pull the data from the datasets. but if it is necessay I’ll do it.

What is important to mention is that 2 weeks ago the page was running smoothly and sudenly something happend and I start getting the 404 Error when I was requesting to see the document with the wixLocation(). This page was develped since almost 3 years running with out issues. It looks like something has been change in the wixLocation(), is that possible?