在 grails 中是否存在真正的异步/ajax 文件上传,它连接到默认的“忙碌”微调器(显示在其他 ajax 调用上的微调器)?
或者,如果我想在文件上传期间显示微调器,我需要明确调用吗?
在 grails 中是否存在真正的异步/ajax 文件上传,它连接到默认的“忙碌”微调器(显示在其他 ajax 调用上的微调器)?
或者,如果我想在文件上传期间显示微调器,我需要明确调用吗?
由于交易所背后的一些复杂性和限制,这是您通常需要第三方帮助 的事情之一。
一些插件可用于 jquery 使用 Flash 来实现这一点,但我更喜欢避免使用 Flash 的任何东西(个人喜好)。查看Valums Ajax-Upload我已经在我的生产站点中使用了大约一年,它运行良好且易于使用。我最喜欢这个插件的一点是它对大多数网站的设计和布局都不显眼。
此外,这是一个常见的问题。看看这些答案以及...
杰夫,
我会这样编码...
如果您想在代码中显示自己,则必须调用微调器。如果要显示图像。
您可以使用 XMLHttpRequest,这是我通过拖放完成的项目的完整代码:
<div id="drop_zone">Drag and drop your file here or click</div>
<input id="curriculumUploader" type="file" name="curriculum" style="display:none;"/>
function uploadCurriculum(file) {
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
//FormData will contain the post params
var form = new FormData();
xmlhttp.onreadystatechange = function() {
//Handle response if everything is right
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//This is the element where i will put my response
var element = document.getElementById('curriculum');
//The response is given in the property responseText
element.innerHTML = xmlhttp.responseText;
//If you response has javascript elements, you need to place them in the header to make client compile them
var scriptElements = element.getElementsByTagName('SCRIPT');
for (i = 0; i < scriptElements.length; i ++) {
var scriptElement = document.createElement('SCRIPT');
scriptElement.type = 'text/javascript';
if (!scriptElements[i].src) {
scriptElement.innerHTML = scriptElements[i].innerHTML;
} else {
scriptElement.src = scriptElements[i].src;
}
document.head.appendChild(scriptElement);
}
}
}
//Put your params in the url if you want to send them by GET
xmlhttp.open("POST", window.appContext + "/controller/action/" + someParams, true);
xmlhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
//add the file to the form to send by POST
form.append('curriculum', file);
xmlhttp.send(form);
}
function handleFileSelect(event) {
handleDragOver(event);
var files = event.target.files || event.dataTransfer.files;
if (files.length > 0) {
parseFile(files[0]);
}
}
function handleDragOver(event) {
event.stopPropagation();
event.preventDefault();
if (event.type == "dragover") {
event.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
addClass(event.target, "hover");
} else {
removeClass(event.target, "hover");
}
}
function showFileSelect() {
document.getElementById('curriculumUploader').click();
}
function parseFile(file) {
if (file != null && file.size > 0) {
uploadCurriculum(file)
} else {
console.log('Invalid file');
}
}
// Setup the dnd listeners.
var dropZone = document.getElementById('drop_zone');
if (dropZone != null) {
dropZone.addEventListener('dragover', handleDragOver, false);
dropZone.addEventListener("dragleave", handleDragOver, false);
dropZone.addEventListener('drop', handleFileSelect, false);
dropZone.addEventListener('click', showFileSelect, false);
}
var inputelement = document.getElementById('curriculumUploader');
inputelement.addEventListener('change', handleFileSelect, false);
#drop_zone {
padding: 15px;
text-align: center;
border: 1px dashed #4b9df2;
color: #4b9df2;
cursor: pointer;
transition: all linear 0.2s;
}
#drop_zone.hover {
border: 1px dashed #C99F23;
color: #C99F23;
}