简体   繁体   English

保存图像对象 VueJS

[英]Save Image Object VueJS

I have an Image Object in VueJS as File {name: "myFile.png", webkitRelativePath: "", size: 120124, …} .我在 VueJS 中有一个图像对象作为File {name: "myFile.png", webkitRelativePath: "", size: 120124, …} I want to save this image into one of my folders.我想将此图像保存到我的一个文件夹中。 How can I do this in VueJS?我怎样才能在 VueJS 中做到这一点?

One more question: Should I save user images to either front-end or back-end?还有一个问题:我应该将用户图像保存到前端还是后端? I can send a request to back-end API but if images are on front-end side it may work fast (correct me if I'm wrong).我可以向后端 API 发送请求,但如果图像在前端,它可能工作得很快(如果我错了请纠正我)。

Note: My front-end (VueJS) and back-end (Laravel as API) are separate projects.注意:我的前端(VueJS)和后端(Laravel as API)是独立的项目。

You have to create an api endpoint on the backend for file saving.您必须在后端创建一个 api 端点以保存文件。 On a frontend you have to send POST request with attached file.在前端,您必须发送带有附件的 POST 请求。

(picture) => {
  const data = new FormData();
  data.append('picture', picture);
  return fetch(
    `${this.$dishApi}/recipes/${recipeId}/pictures/`,
    {
      method: 'POST',
      headers: {
        Accept: 'application/json',
        'x-access-token': localStorage.getItem(this.$dishApiTokenKey),
      },
      body: data,
    },
  )
    .then(
      (res) => res.json(),
    );
},

This function taken from vue component.这个函数取自 vue 组件。 Here FormData object created to store files from the user form.这里创建了 FormData 对象来存储来自用户表单的文件。 You can append several files in this way.您可以通过这种方式附加多个文件。

After appending files post request sent with FormData object inside.附加文件后发送请求,其中包含 FormData 对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM