Hello everyone ![]()
I’m currently stuck trying to upload a PDF to the admin settings > files area. I’m able to upload the PDF but when I try to click on it in the admin, I get the following error modal.
Here are the steps I took so far.
I’m sending the data returned from the form via a POST request to my custom app, which is a WordPress site. Because the app’s “Distribution model” is “Shopify admin”, I’m only authenticating via the access token.
Here’s rough outline of the JS code that runs on the submit event of the form.
const formData = new FormData(e.target)
const formProps = Object.fromEntries(formData)
async function readFileAsync(blob, method = 'readAsDataURL') {
return new Promise((resolve, reject) => {
let reader = new FileReader
reader.onload = () => resolve(reader.result)
reader.onerror = reject
reader[method](blob)
})
}
const fileDataUrl = await readFileAsync(formProps.file)
formData.append('fileDataUrl', fileDataUrl)
// Hereafter, the formData is send via a POST request using Axios.
On the WordPress/PHP side I then continue to call stagedUploadsCreate and send its query parameters plus fileDataUrl (created via JS above) to the URL provided by the response. Then, I call the fileCreate mutation which, again, does upload the file, but the file doesn’t work. My suspicion is that I’m not supplying the correct data to
$file_upload_body[‘file’].
// NOTE $request is an instance of WP_REST_Request.
$file_data_url = $request->get_param('fileDataUrl');
$file = $request->get_file_params();
$file = $file['file'];
$query = <<
And here are the responses I get for the HTTP requests in the above PHP.
```javascript
// Response for stagedUploadsCreate:
{
"code": 200,
"success": true,
"message": "OK",
"body": {
"data": {
"stagedUploadsCreate": {
"stagedTargets": [
{
"url": "https://shopify-staged-uploads.storage.googleapis.com/tmp/xxxxxxxxxxx/files/xxxx-xxxxx-xxxxx-x-xxx-xxx/test.pdf?X-Goog-Algorithm=&X-Goog-Credential=&...",
"resourceUrl": "https://shopify-staged-uploads.storage.googleapis.com/tmp/xxxxxxxxxxx/files/xxxx-xxxxx-xxxxx-x-xxx-xxx/test.pdf",
"parameters": [
{
"name": "content_type",
"value": "application/pdf"
},
{
"name": "acl",
"value": "private"
}
]
}
],
"userErrors": []
}
}
},
"cookies": [],
"headers": {}
}
// Response for the $file_upload_res variable.
// If I run the stagedUploadsCreate via the POST method vs. the PUT, I get a 503 "Service Unavailable" response.
{
"code": 200,
"success": true,
"message": "OK",
"body": "",
"cookies": [],
"headers": {}
}
// Response for the fileCreate mutation.
{
"code": 200,
"success": true,
"message": "OK",
"body": {
"data": {
"fileCreate": {
"files": [
{
"alt": "",
"createdAt": "2022-12-28T14:38:35Z",
"fileStatus": "UPLOADED",
"fileErrors": []
}
],
"userErrors": []
}
}
},
"cookies": [],
"headers": {}
}
Again, I think that I’m not supplying the right value to $file_upload_body[‘file’]. I couldn’t find any documentation on that.
Thanks for any help in advance.
