File Upload using Fetch API - JavaScript Tutorial

Поделиться
HTML-код
  • Опубликовано: 9 июн 2022
  • 👉 Source code: openjavascript.info/2022/06/0...
    ⚡ Looking for high-performance, affordable web hosting for your website or app? We use HostWithLove: bit.ly/3V2RM9Q ❤️
    This tutorial shows you how to upload a file to a server using JavaScript's Fetch API (POST or PUT request). The file and other form data are sent as part of a FormData object.
    #javascript #webdevelopment #tutorial #fetch #fetchapi #fetchapi #httprequest #formdata #ajax #asynchronous
    ⚡ NEW: Web development courses from Meta Inc. ⚡
    Front-End Developer Professional Certificate: imp.i384100.net/b3dMek
    Back-End Developer Professional Certificate: imp.i384100.net/gbYorg
    iOS Developer Professional Certificate: imp.i384100.net/Jr7qj2
    Meta Android Developer Professional Certificate: imp.i384100.net/oeYnGo
    Meta Database Engineer Professional Certificate: imp.i384100.net/BX7KGB
    Website: openjavascript.info
    Twitter: / openjavascript

Комментарии • 41

  • @OpenJavaScript
    @OpenJavaScript  Год назад

    Thanks for watching!
    👉 Here's the source code: openjavascript.info/2022/06/08/how-to-upload-a-file-using-the-fetch-api/

  • @aileenchan3741
    @aileenchan3741 Год назад +3

    Such an easy video to follow! I learned so much!!! Thank you!!!!!! :D

  • @user-te3vp1xt8k
    @user-te3vp1xt8k 11 месяцев назад

    I have an implementation close to this one using a form-data object to an input file we need to send to a REST api which accepts FormData, the problem is in iOS, form some reason when I post to the api in iOS safari the body is set empty and the api fails. Is there a workaround for safari in iOS in order to post form-data objects?

  • @JeyaAnnanathan
    @JeyaAnnanathan Год назад +2

    Excellent tutorial. I have one question. What would happen if the sever has a x-api-key set up? Don't you need to it to add it in the header when you make the POST request?

    • @OpenJavaScript
      @OpenJavaScript  Год назад +1

      Yes, in that case you'd want to set it as a header in a headers property in the options object (second argument position when calling Fetch) as follows:
      headers: {
      "X-Auth-Token": "token-value",
      }

  • @exe.m1dn1ght
    @exe.m1dn1ght 7 месяцев назад

    is the formData.append using streams , fs module and internal buffers ? I don't get it

  • @dimwel
    @dimwel Год назад +1

    thanks

  • @mikaelgevorgyan4521
    @mikaelgevorgyan4521 Год назад

    0o0 nice continue your make videos

    • @OpenJavaScript
      @OpenJavaScript  Год назад +1

      Thanks, there's more to come 😉

    • @mikaelgevorgyan4521
      @mikaelgevorgyan4521 Год назад

      @@OpenJavaScript 🙂☺

    • @melchizedek79
      @melchizedek79 Год назад

      @@OpenJavaScript I really like your approach in explanation. Do you have industry experience? Or already working as a developer?

    • @OpenJavaScript
      @OpenJavaScript  Год назад

      @@melchizedek79 Working as a developer and also creating educational content like this.
      I actually have a strange and windy path to web development as I was originally a university lecturer in a different field for a while. Really enjoyed the teaching and programming aspects, so when that came to an end, became a web developer and, as you can see, now I'm teaching that :)

  • @babachicken333
    @babachicken333 Год назад

    How can I do this by using content-tyoe = application/json, and sending the image base64 to be converted later on ?

    • @OpenJavaScript
      @OpenJavaScript  Год назад +1

      For the request you should add a headers property to the options object (second position in Fetch request):
      headers: {
      "Content-type": "application/json",
      }
      For the reading to base64 you'll want to use the FileReader (tutorial: ruclips.net/video/u2VTtAXq1iA/видео.html)

  • @darkkinggamesyt6463
    @darkkinggamesyt6463 2 года назад +2

    I have a question, I have seen some videos, where they use Ajax to make a uploaded progressive bar. Fecth has something like that? Fetch have a method to get the info while is uploading?

    • @OpenJavaScript
      @OpenJavaScript  2 года назад +1

      Great question!
      I actually wanted to make a video about this. But unfortunately you can't track upload progress using the Fetch API!
      So for now, the native solution is using the XMLHttpRequest object:
      ruclips.net/video/9biknk9SecY/видео.html&t
      Or, for a promise-based solution, using the Axios third-party library (which uses XMLHttpRequest under the hood):
      ruclips.net/video/nC3ntJUQrAM/видео.html
      As soon as it is possible with Fetch API, I will make a video :)

    • @darkkinggamesyt6463
      @darkkinggamesyt6463 2 года назад

      @@OpenJavaScript Thanks, in general which is better?

    • @OpenJavaScript
      @OpenJavaScript  2 года назад

      @@darkkinggamesyt6463 I prefer XMLHttpRequest because it doesn't require importing anything.
      But Axios more straightforward promise-based syntax if that's more important to you.

  • @Cubas1111
    @Cubas1111 Год назад +1

    Nice video! I'm working with Django and I get this error ""415 (Unsupported Media Type)"" I already google as much as i can and tried different code but without success. Any advice please!

    • @OpenJavaScript
      @OpenJavaScript  Год назад

      From the error it looks like your server is rejecting the data for some reason.
      Sometimes, this is to do with the 'Content-Type' header. Sometimes, there can be a problem if your payload is a FormData object and you set it manually, so if you have one, try removing it.
      If it isn't working without, try adding one by including the following property on the options object, adjusting for the file contents:
      headers: {
      'Content-Type': 'multipart/formdata'
      }
      Another header you could try:
      'application/octet-stream'
      Or, if you are sending a raw File object, the MIME type that corresponds to the File you are sending.

  • @skizzerop9079
    @skizzerop9079 Год назад +1

    I have a Question regarding where the files are saved are they saved in the directory from the fetch or where exactly are they getting saved when uploaded to the server?

    • @OpenJavaScript
      @OpenJavaScript  Год назад

      Good question!
      According to the httpbin source code documentation, it should just echo the data you send it back to you. So it should only be held in memory as long as your request is being handled without being stored anywhere permanently.
      However, to be on the safe side, I would still not send any sensitive data as it is ultimately a third-party server.

  • @8koi245
    @8koi245 Год назад +1

    Pretty cool explanation, lil too much for my taste, but then where I can actually upload to images to? I only know about ipfs lmao

    • @OpenJavaScript
      @OpenJavaScript  Год назад

      You can upload the image to any server that will accept an image as an upload.
      For example, you could set up a POST endpoint using Node.js + Express and save the file locally or from there post it to a database or cloud solution.
      Will consider doing a tutorial on this in the near future, as this is an interesting question!

  • @_nigerianprince
    @_nigerianprince Год назад +1

    I am getting 405 error. Essentially, the I am being denied to send a post request. Could it be because i am using vscode liver-sever?. Also, I want to upload image to an Upload folder I created. Will the fetch api work?

    • @OpenJavaScript
      @OpenJavaScript  Год назад

      A 405 error is probably not live-server related (though it wouldn't hurt to try without). More likely you are making a request to an endpoint that doesn't support that method (e.g. GET, POST).
      Concerning the file upload, yes you can! But you'll also need to write the code to handle to saving of the file on the server-side (e.g. using Node.js).
      Here's a tutorial on working with the FS module in Node.js: ruclips.net/video/dtTnWxfH19U/видео.html

  • @jk_batham
    @jk_batham Год назад +1

    how to submit text data and file data using javascript fetch

    • @OpenJavaScript
      @OpenJavaScript  Год назад

      You can use the FormData object like in this video.
      Make sure the text and file input have a "name" attribute. Then they'll be included in the new FormData object you create when passing the form element into it.
      The payload you send is then this object.

  • @olawumisegun5898
    @olawumisegun5898 Год назад +1

    How can I send an array of object containing image and text input using formdata. I have a node js backend api for book app, each books can have several authors. How do I pass this dynamic data to my backend.
    [{name: "First author name ", image: "First author image file here "},
    {name: "Second author name", image: "Second author image file here }]

    • @OpenJavaScript
      @OpenJavaScript  Год назад

      I would probably 'stringify' the image to base64 format. Then call JSON.stringify() on the object and send it as a pure string OR embed the string in a Blob of type application/json.
      To stringify the image, you can use the FileReader, which I covered in this tutorial: ruclips.net/video/u2VTtAXq1iA/видео.html

    • @olawumisegun5898
      @olawumisegun5898 Год назад +1

      @@OpenJavaScript jpeg or png

    • @OpenJavaScript
      @OpenJavaScript  Год назад

      @@olawumisegun5898 I woulf probably go with the stringify solution that I mentioned in the previous comment (edited it while you were replying I think)

    • @olawumisegun5898
      @olawumisegun5898 Год назад

      @@OpenJavaScript OK

  • @opeyemialatishe1944
    @opeyemialatishe1944 Год назад +1

    The video isn't clear enough... I mean not well explained

    • @OpenJavaScript
      @OpenJavaScript  Год назад +1

      Thanks for the feedback.
      Is there a particular part or aspect that you found I didn't explain well? I'm always looking to improve the tutorials!

    • @opeyemialatishe1944
      @opeyemialatishe1944 Год назад +1

      @@OpenJavaScript finally gotten it....
      Thanks for the support 🫡

    • @OpenJavaScript
      @OpenJavaScript  Год назад

      @@opeyemialatishe1944Great that you found a solution!