簡體   English   中英

通過 API 到 postman 使用 Rails 主動存儲上傳文件(.pdf、.jpg 等)? (不通過 Rails 視圖)

[英]upload a file(.pdf, .jpg etc) using rails active storage via API through postman? (not through rails views)

我看到了N個關於使用rails view將文件上傳到本地活動存儲、S3 等的教程。 But I cannot find a legit source on how to upload a file to an active storage through API Say via postman, like what are prerequisites required to pass an attachment through API via postman?

讓我詳細說明一下。

第1步:通過React或任何前端框架,我將選擇一個文件上傳(a.PDF文件),最終它應該保存在某個地方。

第 2 步:這個選擇的文件應該作為 API 傳遞到后端服務,在該后端服務中,選擇的文件的存儲將保存在 AWS S3 等存儲中。

如何發送 API 請求來存儲文件? 有人可以幫我解決嗎?

打開 Postman 非常簡單
Go 到 Body -> Form-data 然后將鼠標移到關鍵字段,您將找到 File Option

Postman 中的文本和文件選項

然后你必須 select 文件並發送請求在此處輸入圖像描述

Or send the File Path in the raw JSON object as well like this and then Create the Object at backend so place check on file object if it blank then send path like this

   // For Temporary Use Over Back End Only NOT NEED To Send These 
    "file_path" : "/home/jolly/Downloads/pdf/pdf-5.pdf",
    "file_name" : "pdf-5.pdf"

然后在代碼中使用它來創建文件 object ,您將其傳遞到您需要保存 s3 或 rails 存儲的位置

    file_path = params[:file_path]
    file_name = params[:file_name]
    image_path = Rails.root+file_path
    image_file = File.new(image_path)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM