将图像文件数据从前端发送到后端

时间:2017-08-24 18:34:50

标签: node.js file image-uploading cloudinary

有没有办法将图像文件从前端发送到后端?我需要将表单中选择的图像上传到云端云平台。

使用req.body访问文件输入无济于事,并向我显示错误no such file or directory open

如果我添加enctype="multipart/form-data",然后在我的后端执行req.on('data', (data)=> {console.log(data.toString)}),然后执行上传到cloudinary,它会失败并给我以下错误:Error: Path must be a string without null bytes

这是我的表格:

<form method="post" action="/submitArticle" enctype="multipart/form-data">
    <div class="file">
      <label class="file-label">
        <input class="file-input" type="file" name="image">
        <span class="file-cta">
          <span class="file-icon">
            <i class="fa fa-upload"></i>
          </span>
          <span class="file-label">
            Choose a file…
          </span>
        </span>
      </label>
    </div>
    <div class="field">
      <label class="title is-4">Title</label>
      <div class="control">
        <input class="input" name="title" type="text" placeholder="Give your story a title...">
      </div>
    </div>
    <div class="field">
      <label class="title is-4">Story</label>
      <div class="control">
        <textarea class="textarea" name="story" rows="7" placeholder="Tell your story here..."></textarea>
      </div>
    </div>
    <div class="field is-grouped is-grouped-right">
      <p class="control">
        <input class="button is-primary" type="submit">
        </input>
      </p>
    </div>
  </form>

还有另一种方法可以将图像数据从前端发送到后端吗?在此先感谢!!

2 个答案:

答案 0 :(得分:1)

如果您想上传到Cloudinary,也许您应该考虑使用Cloudinary Upload Widget

小部件支持将图像直接从浏览器上传到Cloudinary存储,而不会让您的服务器参与进程。

答案 1 :(得分:0)

您可以使用Dropzone之类的第三方库在上端上传图片并将其发送到后端。简单就是这样

相关问题