Jquery根据文件类型上传到预设URL
我需要一个上传系统,根据文件类型上传到特定的预设URL,例如: 图像将上载到“exampleserver.com/upload/image”,视频将上载到“exampleserver.com/upload/video” 我看了一下,但无法修改代码(可能是缺少JQuery插件方面的经验) 上传系统确实需要支持多个文件选择,并具有跨浏览器兼容性(IE6除外) 非常感谢对上述Jquery文件上传插件代码的任何建议或修改Jquery根据文件类型上传到预设URL,jquery,upload,Jquery,Upload,我需要一个上传系统,根据文件类型上传到特定的预设URL,例如: 图像将上载到“exampleserver.com/upload/image”,视频将上载到“exampleserver.com/upload/video” 我看了一下,但无法修改代码(可能是缺少JQuery插件方面的经验) 上传系统确实需要支持多个文件选择,并具有跨浏览器兼容性(IE6除外) 非常感谢对上述Jquery文件上传插件代码的任何建议或修改 编辑:上传图像和视频的服务器不是我的,它们是Facebook的。在查看了您链接到的
编辑:上传图像和视频的服务器不是我的,它们是Facebook的。在查看了您链接到的文件上传组件的文档后,它说“适用于任何服务器端平台”,因此您仍然需要在服务器端实现一些功能。这里有几个开始的链接: 简单介绍:
php手动输入:为了满足客户对HTML5支持、文件、文件列表和Blob的需求,您需要使用它。您可以看到这是如何工作的 在上传程序中更改设置非常简单:(查找“选项”) 我建议您将图像和视频提交到相同的位置,并在服务器端进行检查。那会让你的生活更轻松 编辑:
以下是如何检查扩展并将图像和视频提交到不同的脚本:
function checkFileExtension(file) {
var extension = file.name.split('.').pop().toLowerCase();
var image_extensions = ['gif', 'png', 'jpg', 'jpeg'];
var video_extensions = ['mp4', 'avi', 'wmv'];
// The file extension is not in the array
if ($.inArray(extension, image_extensions) >= 0)
return "image";
else if ($.inArray(extension, video_extensions) >= 0)
return "video";
else
return "notAllowed";
}
// Initialize the jQuery File Upload widget:
$('#fileupload').fileupload({
// This is triggered when you drag'n'drop a file over the uploader
drop: function (e, data) {
$.each(data.files, function (index, file) {
// The file doesn't pass checkFileExtension, return an error
var extension = checkFileExtension(file);
if (extension != "image" && extension != "video") {
// Print an error message in the UI
file.error = "File extension not allowed!";
}
});
},
// This is triggered when you click a button and select files from a list.
change:function (e, data) {
$.each(data.files, function (index, file) {
// The file doesn't pass checkFileExtension, return an error
var extension = checkFileExtension(file);
if (extension != "image" && extension != "video") {
// Print an error message in the UI
file.error = "File extension not allowed!";
}
});
},
// This is triggered on every file in the queue when you click "Upload"
submit: function (e, data) {
// The file is an image - submit those to
if (checkFileExtension(data.files[0]) == "image") {
$('#fileupload').fileupload(
'option',
'url',
'/upload/images'
);
}
// The file is a video.
else if(checkFileExtension(data.files[0]) == "video"){
$('#fileupload').fileupload(
'option',
'url',
'/upload/videos'
);
}
// The file is not a video/image - don't submit the file.
else {
return false;
}
}
});
您正在运行什么类型的服务器?上传文件的目的地将由服务器端http处理程序确定,如PHP、ASP.NET、RoR等。我使用PHP运行apache服务器,但是文件将上传到facebook的服务器,但出于某种奇怪的原因,你需要将视频和图像上传到另一个GraphAPI URL facebook是否让你将上传控件放入带有附加信息的标签中,以将其发送到他们的服务器,还是以其他方式发送给他们?如果是这种情况,您可以使用javascript确定文件的类型,并更新他们希望您随文件发送的任何隐藏表单字段,以告诉他们如何处理该文件。同样,也不确定是否是这样。没有facebook与上传系统无关,它只是你使用Graph API将文件发送到的终点;这只是为了我正在开发的网站我需要一个基于JQuery的上传系统对不起,我不清楚这个问题,但是上传服务器不是我的,但是FaceBooks也需要跨浏览器支持,IE7-9还没有(完全)支持HTML5。你唯一的办法就是在服务器端进行检查。不,不是,如果您构建了一个文件扩展名检查,并基于此更改上传url,这是可能的,我只是不知道JQuery插件要做什么,所以我已经更新了我的答案。这是检查文件扩展名的方法。请注意,这是不可靠的。我可能有一个
PDF
文件,然后将其重命名为JPG
文件,检查就会通过。很抱歉,我不清楚这个问题,但上传服务器不是我的,而是facebook。对了,你的最终目标是将文件上传到facebook的服务器。根据我在这篇文章中看到的情况,我认为你必须先将文件发送到你自己的服务器,除非facebook有一个界面,你可以直接将文件上传到他们那里。非常怀疑这是唯一的方法,应该有一个上传插件并修改它以检查每个文件上传到哪里
function checkFileExtension(file) {
var extension = file.name.split('.').pop().toLowerCase();
var image_extensions = ['gif', 'png', 'jpg', 'jpeg'];
var video_extensions = ['mp4', 'avi', 'wmv'];
// The file extension is not in the array
if ($.inArray(extension, image_extensions) >= 0)
return "image";
else if ($.inArray(extension, video_extensions) >= 0)
return "video";
else
return "notAllowed";
}
// Initialize the jQuery File Upload widget:
$('#fileupload').fileupload({
// This is triggered when you drag'n'drop a file over the uploader
drop: function (e, data) {
$.each(data.files, function (index, file) {
// The file doesn't pass checkFileExtension, return an error
var extension = checkFileExtension(file);
if (extension != "image" && extension != "video") {
// Print an error message in the UI
file.error = "File extension not allowed!";
}
});
},
// This is triggered when you click a button and select files from a list.
change:function (e, data) {
$.each(data.files, function (index, file) {
// The file doesn't pass checkFileExtension, return an error
var extension = checkFileExtension(file);
if (extension != "image" && extension != "video") {
// Print an error message in the UI
file.error = "File extension not allowed!";
}
});
},
// This is triggered on every file in the queue when you click "Upload"
submit: function (e, data) {
// The file is an image - submit those to
if (checkFileExtension(data.files[0]) == "image") {
$('#fileupload').fileupload(
'option',
'url',
'/upload/images'
);
}
// The file is a video.
else if(checkFileExtension(data.files[0]) == "video"){
$('#fileupload').fileupload(
'option',
'url',
'/upload/videos'
);
}
// The file is not a video/image - don't submit the file.
else {
return false;
}
}
});