2017-05-08 41 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请求的处理函数的事件。