Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Example of upload file via Node is not working #926

Closed
Tracked by #1948
brunomateus opened this issue Jun 10, 2022 · 10 comments
Closed
Tracked by #1948

[Bug]: Example of upload file via Node is not working #926

brunomateus opened this issue Jun 10, 2022 · 10 comments
Assignees
Labels
source: Dev Docs PRs/issues targeting the Developer Docs target: v4 Documentation PRs/issues targeting content from docs.strapi.io (main branch).

Comments

@brunomateus
Copy link

brunomateus commented Jun 10, 2022

Link to the documentation page or resource

https://docs.strapi.io/developer-docs/latest/plugins/upload.html#upload-files

Describe the bug

The sample of Node.Js code showed on that page is not working. It's always give you HTTP 400 and this messages "message":"Files are empty".

I spent long hours trying, then I decide to write something similar in Python and that's it, it worked at the first time.

Additional context

No response

Suggested improvements or fixes

I am not sure what is the current bug, it seems that form.append('files', file) is not working, but we need to fix that.

Also, I do believe that an example of code using entity or service API would be a great help. This is really useful when we need do populate a database.

Related issue(s)/PR(s)

No response

@brunomateus brunomateus added the status: pending reproduction Waiting for free time to reproduce the issue, or more information label Jun 10, 2022
@harris2310
Copy link

Hello there, I could resolve this issue for you!

@pwizla
Copy link
Collaborator

pwizla commented Jun 20, 2022

Hello @brunomateus, and thank you for opening this issue. I'm sorry you are having trouble. Our team is indeed currently reworking the Upload documentation as it's not up-to-date with Strapi v4.

That's so nice of you to offer some help @harris2310! ❤️ We really appreciate it!

I'm assigning @StrapiShaun to this issue as he's in charge of the rewrite for this documentation, and I'll let him get in touch with you to solve this issue together. Thank you again!

Edit: @gpene is actually the technical writer working on Upload docs, re-assigning the issue :-)

@pwizla pwizla added source: Dev Docs PRs/issues targeting the Developer Docs target: v4 Documentation PRs/issues targeting content from docs.strapi.io (main branch). and removed status: pending reproduction Waiting for free time to reproduce the issue, or more information labels Jun 20, 2022
@pwizla pwizla assigned gpene and unassigned stb13579 Jun 20, 2022
@gpene gpene mentioned this issue Jul 4, 2022
1 task
@gpene
Copy link
Contributor

gpene commented Jul 5, 2022

@brunomateus Looks like this may have been addressed already just a few days after you reported the issue, if you could give it another go and confirm: https://github.com/strapi/documentation/pull/908/files
Thanks!

@brunomateus
Copy link
Author

Hi everyone, sorry for the late reply.
I just tried again and it worked fortunately. Great job!

I have just few notes:

  • First I tried using a Node + Typescript (I prefer ts than js) project and I could not make it work because I was targeting commonjs and I had problems to import blobFrom.
  • I put type: module in my package.json and changed from module: commonjs to module: esnext in my tsconfig.json, but I got other errors.
  • I gave up of using Ts and I still had problem because I was using localhost, I got connection refused until I changed to 127.0.0.1.

@gpene
Copy link
Contributor

gpene commented Jul 6, 2022

Thanks for the additional notes @brunomateus - will keep those in mind as we revise the current documentation.
As the original problem reported here is resolved will close this issue - thanks again!

@gpene gpene closed this as completed Jul 6, 2022
@nagman
Copy link

nagman commented Nov 16, 2023

Hi there,
The issue is still not solved as I have exactly the same: I'm using TS and blobFrom can't be used.

As I really need typescript for this project, I'm stuck. I've tried also sending a file like this:

import { FormDataEncoder } from "form-data-encoder";
import { FormData } from "formdata-node";
import { fileFromPath } from "formdata-node/file-from-path";
import fetch from "node-fetch";
import { join } from "path";
import { Readable } from "stream";

// ...

export async function upload() {
  // ...

  const path2img = join(__dirname, "uploads", image.src);

  const file = await fileFromPath(path2img);
  const form = new FormData();
  form.set("files", file);
  const encoder = new FormDataEncoder(form);

  const response = await fetch("http://127.0.0.1:1337/upload", {
    method: "POST",
    body: Readable.from(encoder),
    headers: { Authorization: `Bearer ${process.env.STRAPI_API_TOKEN}` },
  });

  // ...
}

And I'm getting a 400 error: Files are empty.

I've checked my file and it does exists, with the correct filesize in bytes, yet Strapi seems to ignore it.

It's been hours I'm stuck with this and none of the solutions I've found on internet seem to work :'(

@nagman
Copy link

nagman commented Dec 1, 2023

@gpene could you reopen this thread please? The issue has never been solved.
There actually is no working example of how to upload a file through node.js with strapi.
I'm struggling for hours, I tried many things, none of them work. I always get a "Files are empty".

But when intercepting the request with mitmproxy, I can see the octet-stream of my file under the name "files".

@pwizla pwizla assigned pwizla and unassigned gpene Dec 1, 2023
@pwizla
Copy link
Collaborator

pwizla commented Dec 1, 2023

Hi @nagman. "gpene" is not working for Strapi anymore. I'll check with Strapi engineers what we can do for this one.

@pwizla pwizla reopened this Dec 1, 2023
@pwizla
Copy link
Collaborator

pwizla commented Dec 11, 2023

Hi,
Thanks again for reporting. I'm closing this issue for now, but it's included in the meta issue for the Upload plugin (#1948). More details in the linked issue :)

@nagman
Copy link

nagman commented Jan 8, 2024

I managed to fix it by:

  • installing fix-esm
  • installing node-fetch@^3.3.2
  • importing node-fetch like this: const fetch = require("fix-esm").require("node-fetch");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
source: Dev Docs PRs/issues targeting the Developer Docs target: v4 Documentation PRs/issues targeting content from docs.strapi.io (main branch).
Projects
None yet
Development

No branches or pull requests

6 participants