首页 编程技术在NodeJS中使用jQuery file upload plugin

在NodeJS中使用jQuery file upload plugin

运维派隶属马哥教育旗下专业运维社区,是国内成立最早的IT运维技术社区,欢迎关注公众号:yunweipai
领取学习更多免费Linux云计算、Python、Docker、K8s教程关注公众号:马哥linux运维

https://github.com/blueimp/jQuery-File-Upload 是一个相对完备的Web文件上传组件。这个组件不仅具备文件上传的功能,还具备远程文件管理的功能。目前是最流行的jQuery文件上传组件。
这个组件只有客户端,没有服务端,需要开发者自己提供服务端。在其Sample中有一个NodeJS的服务端,不过是一个独立服务器,如果要集成到自己的已有的NodeJS程序中,还需要花费一番功夫。
最开始我想把它提供的Sample NodeJS代码修改,集成到我自己的服务器中,不过读了一遍Sample代码之后,我就放弃了,太复杂了。
不过在其README中介绍了一个中间件,很容易与其界面集成:https://github.com/aguidrevitch/jquery-file-upload-middleware
用上了之后,发现虽然文件上传成功了,但是界面上显示失败:“Empty file upload result”。到Google搜索,有人说是jQuery file upload plugin的接口变了,由:[{file1},{file2}] 变为了 {files: [{file1},{file2}]}。
通过抓包发现后台返回的数据是
[
{
“name”: “iPhone5 docking station.jpeg”,
“originalName”: “iPhone5 docking station.jpeg”,
“size”: 9584,
“type”: “image/jpeg”,
“delete_type”: “DELETE”,
“url”: “http://localhost/uploads/iPhone5%20docking%20station.jpeg”,
“delete_url”: “http://localhost/product/upload/iPhone5%20docking%20station.jpeg”,
“thumbnail_url”: “http://localhost/uploads/thumbnail/iPhone5%20docking%20station.jpeg”
}
]
找到https://github.com/aguidrevitch/jquery-file-upload-middleware的源代码,修改返回数据为新的格式后,果然成功了。新的数据格式:
{ files:
[
{
“name”: “iPhone5 docking station.jpeg”,
“originalName”: “iPhone5 docking station.jpeg”,
“size”: 9584,
“type”: “image/jpeg”,
“delete_type”: “DELETE”,
“url”: “http://localhost/uploads/iPhone5%20docking%20station.jpeg”,
“delete_url”: “http://localhost/product/upload/iPhone5%20docking%20station.jpeg”,
“thumbnail_url”: “http://localhost/uploads/thumbnail/iPhone5%20docking%20station.jpeg”
}
]
}

我已经提了一个issue:https://github.com/aguidrevitch/jquery-file-upload-middleware/issues/23,目前查看最新的源代码,已经修正了这个问题。有兴趣的朋友,到github上follow me,哈哈 @ibusybox

本文链接:https://www.yunweipai.com/2831.html

网友评论comments

发表回复

您的电子邮箱地址不会被公开。

暂无评论

Copyright © 2012-2022 YUNWEIPAI.COM - 运维派 京ICP备16064699号-6
扫二维码
扫二维码
返回顶部