开发工具 swfupload 上传多图片在 chrome 上正常,safari 和 firefox 上出错报的是日志显示 401 错误

fayake · 2015年07月12日 · 最后由 fayake 回复于 2015年07月23日 · 2943 次阅读

swfupload 上传多图片在 chrome 上正常,safari 和 firefox 上出错报的是日志显示 401 错误 下面是日志信息

Started POST "/ reply_messages/upload_ajax" for 127.0.0.1 at 2015-07-12 20:15:54 +0800
Processing by ReplyMessagesController#upload_ajax as HTML
  Parameters: {"Filename"=>"1.pic.jpg", "uploadfile"=>#<ActionDispatch::Http::UploadedFile:0x007fc4dd789480 @original_filename="1.pic.jpg", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"uploadfile\"; filename=\"1.pic.jpg\"\r\nContent-Type: application/octet-stream\r\n", @tempfile=#<File:/var/folders/27/2ykltwhs56jdgcd51hz4wz1w0000gn/T/RackMultipart20150712-43675-e2jmrj>>, "Upload"=>"Submit Query"}
Completed 401 Unauthorized in 1ms

网上有说是因为 cookie 没有传到服务器的问题 我在 swfupload 中加入了

post_params:{'session_id': '<%= cookies[:_session_id] %>','authenticity_token':'<%= form_authenticity_token.to_s %>'},

完整如下

$('#myModal #swfupload-control').swfupload({
                upload_url: "/ reply_messages/upload_ajax",
                post_params:{'session_id': '<%= cookies[:_session_id] %>','authenticity_token':'<%= form_authenticity_token.to_s %>'},

                file_post_name: 'uploadfile',
                file_size_limit : "2048",
                file_types : "*.jpg;*.bmp;*.jpeg;*.png;*.gif",
                file_types_description : "Image files",
                file_upload_limit : 5,
                flash_url : "/jquery-swfupload/js/swfupload/uploadify.swf",
                button_image_url : '/img/wdp_buttons_upload_114x29.png',
                button_width : 114,
                button_height : 29,
                button_placeholder : $('#myModal #button')[0],
                debug: false
            })

但还是相同问题 请问谁怎么要怎么改 知道的麻烦回复下感谢

看 Rails 日志,已定是 CSRF 验证的问题

@ fayake 我之前用 jquery fileupload 实现文件的异步上传,也遇到了你类似的问题 正如 @huacnlee 所说,是 CSRF token 的问题,请按如下方式设置 ajax 的 headers

$.ajaxSetup({ headers: { 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') } });

#2 楼 @luolinae86 感谢已经解决了谢谢!!!

需要 登录 后方可回复, 如果你还没有账号请 注册新账号