zl程序教程

您现在的位置是:首页 >  前端

当前栏目

在NodeJS中使用jQuery file upload plugin

NodejsjQuery 使用 File plugin Upload
2023-06-13 09:15:43 时间

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

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

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/53196.html

googlejava