0
我有一個Base64編碼的圖片進入我的應用程序。我想在其他地方重新發布該映像,但它將內容類型設置爲目標上的多部分/表單數據。我如何上傳這張圖片?Rails的帖子圖片
file_name = permitted_params[:file_name]
file_contents = permitted_params[:file_contents]
file = Tempfile.new(file_name)
file.binmode
file.write(Base64.decode64(file_contents))
file.rewind()
raw_response = RestClient.put(
url,
{ 'upload' => file, :content_type => 'image/jpeg' },
:headers => {:content_type => 'image/jpeg'}
)
UPDATE(解決)
我需要使用RESTClient實現,因爲我需要通過將它傳遞到另一個服務器(因此在PUT 'URL')。
我的問題是在圖像解碼我不剝出
data:image/jpeg;base64,
然後用這個代碼:
raw_response = RestClient.put(url,
file_binary,
{:content_type => imageContentType})
我能得到它把圖像和設置正確的內容類型。下面的答案確實有幫助,因爲我試圖確保圖像正確解碼,而不是。