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
183 views
in Technique[技术] by (71.8m points)

How can i upload files to file.io using cloud flare workers[javascript]?

These recent days I tryed multiple times to upload file to file.io using cfworkers but it returns error this is my source code:


  async function handleRequest() {
const file = fetch('https://upload.wikimedia.org/wikipedia/commons/thumb/b/b1/02_start_book_creator.png/640px-02_start_book_creator.png');
let formData = new FormData();
formData.append("file", file);
 var returned=fetch('https://file.io', {
  method: 'POST',
  body: formData
});


 return returned
    }
    addEventListener("fetch", event => {
      return event.respondWith(handleRequest())
      })

The error is this:

{"success":false,"error":400,"message":"Trouble uploading file"}

What i have to do with this?

question from:https://stackoverflow.com/questions/65650866/how-can-i-upload-files-to-file-io-using-cloud-flare-workersjavascript

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

1 Answer

0 votes
by (71.8m points)

Unfortunately, as of this writing, Cloudflare Workers doesn't support the File API and thus can't construct FormData containing filenames and types, which is probably what you need here.

However, coincidentally, we're planning to roll out support for this very soon -- this week or next week. Once we've done that, you can construct your FormData containing File objects, like so:

let response = await fetch('https://upload.wikimedia.org/wikipedia/commons/thumb/b/b1/02_start_book_creator.png/640px-02_start_book_creator.png');
let data = await response.arrayBuffer();
let file = new File([data], "640px-02_start_book_creator.png", {type: "image/png"})
let formData = new FormData();
formData.append("file", file);

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

...