2017-05-08 73 views
0

我有一個終點,我可以這樣上傳文本文件,且捲曲:用的multipart/form-data的使用CLJ-AJAX HTTP POST請求

curl -X POST -H "Content-Type: multipart/form-data" -F "[email protected]/resources/speciesDiffusion.tree" http://localhost:4000/continuous/tree 

現在我需要從瀏覽器中發送了類似的請求但

(ajax/ajax-request 
    {:uri (str "http://localhost:4000" "/continuous/tree") 
    :method :post 
    :params {:treefile file} 
    :handler #(println %1) 
    :format (ajax/text-request-format) 
    :response-format (ajax/json-response-format {:keywords? true})}) 

給了我一個(JSON很好的轉化,所以我得到的那部分去,這是很好的)錯誤響應:

[false {:status 500, :status-text , :failure :error, :response {:timestamp 1494279686227, :status 500, :error Internal Server Error, :exception org.springframework.web.multipart.MultipartException, :message Current request is not a multipart request, :path /continuous/tree}}] 

一另外,在瀏覽器中,我可以看到內容類型標題沒有正確設置,但是我無法使用其他格式和:params的其他組合。

回答

0

在cljs-ajax項目的README中有一些例子。例如:

(let [form-data (doto 
        (js/FormData.) 
        (.append "id" "10") 
        (.append "file" js-file-value "filename.txt"))] 
    (POST "/send-file" {:body form-data 
         :response-format (raw-response-format) 
         :timeout 100})) 

https://github.com/JulianBirch/cljs-ajax

+0

謝謝,但我很清楚現有的文檔,但不幸解決不了問題。 – fbielejec

+0

我建議再讀一遍,因爲你的例子沒有指定':body'或任何':headers' – acron

+0

根據官方文檔::params - 與請求一起發送的參數,格式相關的::transit和: edn可以發送任何東西,:json和:raw需要給出一張地圖。 GET會將參數添加到查詢字符串中,POST會將參數放入正文中。 – fbielejec

0

按我的意見,問題並不在請求中,而是在F-重刑分派它,即我在讀文件內容而不是發送原始對象喜歡這裏:

(defn handle-file-upload [evt] 
    (let [target (.-currentTarget evt) 
     js-file (-> target .-files (aget 0))] 
    (do 
     (re-frame/dispatch [:post-file {:file js-file 
               :name (.-name js-file)}]) 
     (set! (.-value target) "")))) 

(defn upload-button [] 
    (fn [] 
[:input {:class "none" 
      :id "file-upload" 
      :type "file" 
      :on-change #(handle-file-upload %)}])) 

其中

:後文件

是調用執行POST請求的處理函數的事件。