search

Home  >  Q&A  >  body text

javascript - JS file upload problem

Question:
1. Pictures can be converted to base64 for uploading, but why can’t Excel, PDF, etc. be converted to base64?
2. Upload a file through XMLHttpRequest and put the file into FormData. But how to bind a tag to the file? For example, how do I know that this file belongs to my current user?
3. Does anyone have a good js file upload solution?

淡淡烟草味淡淡烟草味2737 days ago1016

reply all(4)I'll reply

  • 三叔

    三叔2017-07-05 11:10:28

    1. In modern browsers, it can be implemented using html5 related APIs. I temporarily think of these Blob FileReader btoa. You need to check the documentation for details

    2. FormData can add additional fields with append. You need to open your mind here. A field ultimately represents a parameter passed to the backend. This parameter is actually a very abstract concept, and it depends on the agreement between your frontend and the backend.
      For example, when I want to send the request at the end, I can construct an additional object (its field values ​​need to be discussed with the background), add it to FormData, and send it to the server.

         var meta = {
                name:"",
                time:"",
                data:[]
            }
        formData.append("meta", JSON.stringify(meta));

    The server finally deserializes this field into an object and obtains the value inside.

    Finally, js file upload is a function provided after HTML5, so there are compatibility issues. To put it bluntly, it is the use of those new APIs. Go to MDN and take a look at the relevant documents and demos and it will be almost done

    reply
    0
  • 扔个三星炸死你

    扔个三星炸死你2017-07-05 11:10:28

    1.pdf should be convertible to base64. Excel has not been tested, but excel can be converted to Blob type. In theory, it should be converted to base6.

    2.FormData adds another parameter as the unique mark of the file, and then passes it to the backend together.

    reply
    0
  • typecho

    typecho2017-07-05 11:10:28

    I’m not sure about the first point, but the second point needs to go through the session and rely on the server

    reply
    0
  • 扔个三星炸死你

    扔个三星炸死你2017-07-05 11:10:28

    The second point is to add an input field to the formdata

    reply
    0
  • Cancelreply