简体   繁体   English

使用 Javascript 上传到 Rails 活动存储

[英]Upload to Rails Active Storage Using Javascript

I am attempting to upload an image to active storage in my Rails backend.我正在尝试将图像上传到我的 Rails 后端的活动存储中。 I currently have the backend setup correctly but I am struggling to understand FormData.我目前的后端设置正确,但我很难理解 FormData。 Here is what I have.这就是我所拥有的。

The on change function is listening for a change in the input which is of type 'file'.更改 function 正在侦听“文件”类型的输入中的更改。 When I add the photo to the input I can get the info from e.target... However I don't know what to add as the URI here.当我将照片添加到输入时,我可以从 e.target 获取信息......但是我不知道在此处添加什么作为 URI。 The e.target.value here is a uri that is protected.这里的 e.target.value 是一个受保护的 uri。 So I'm confused about how this is supposed to work:所以我对这应该如何工作感到困惑:

By the way, setFile is just setting 'file' to that object.顺便说一句,setFile 只是将“文件”设置为 object。 I am using react.我正在使用反应。

const onChange = (e) => {
    console.log(e.target.files)
    setFile({
        uri: e.target.value,
        name: e.target.files[0].name,
        type: e.target.files[0].type
    })
}

const onSubmit = () => {
    console.log(file)

    let imageURI = file.uri
    let formdata = new FormData();

    formdata.append("image", { uri: imageURI, name: `${file.name}`, type: `${file.type}` })
    formdata.append("image", file)

    requests.postImageToCurriculum(66, formdata)
}

if you are using Active storage, use their js package如果您使用的是活动存储,请使用他们的 js package



import { DirectUpload } from "@rails/activestorage"

class Uploader {
  constructor(file, url) {
    this.upload = new DirectUpload(this.file, this.url, this)
  }

  upload(file) {
    this.upload.create((error, blob) => {
      if (error) {
        // Handle the error
      } else {
        // Add an appropriately-named hidden input to the form
        // with a value of blob.signed_id
      }
    })
  }

  directUploadWillStoreFileWithXHR(request) {
    request.upload.addEventListener("progress",
      event => this.directUploadDidProgress(event))
  }

  directUploadDidProgress(event) {
    // Use event.loaded and event.total to update the progress bar
  }
}


link here 链接在这里

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

相关问题 使用 Rails 中的活动存储直接上传 - input.dataset.directUploadUrl “未定义” - direct upload using active storage in rails - input.dataset.directUploadUrl “undefined” 当 MIME 类型为空时,Rails Active Storage 会阻止上传 - Rails Active Storage prevents upload when mime type is empty Active Storage无法使用AJAX Rails 5.2上传图像附件 - Active Storage failing to upload image attachment with AJAX Rails 5.2 Active Storage Direct Upload 正在使用简单表单的上传期间删除文件输入 - Rails 6 - Active Storage Direct Upload is removing the file input during upload w/Simple Form - Rails 6 使用 Firebase Storage 和 Dropzone 使用 JavaScript 上传 - Upload with JavaScript using Firebase Storage and Dropzone 使用 javascript 在 Rails 3 中创建一个文件上传 - Create a file for upload in rails 3 using javascript 使用JavaScript从客户端浏览器上传到Google云端存储 - Upload from Client Browser to Google Cloud Storage Using JavaScript 使用javascript将请求批量上传到Google云端存储 - Batch upload requests to Google Cloud Storage using javascript 如何使用JavaScript将图像上传到Azure Blob存储 - How to upload image to azure blob storage using javascript 如何使用 Javascript 将多个文件上传到 Azure 存储 - How to upload multiple files to Azure Storage using Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM