是否有真正的异步/ ajax文件在grails中上传,这些文件挂钩到默认的“busy”微调器(在其他ajax调用上显示的那个)?
或者,如果我想在文件上传期间显示微调器,我需要明确调用吗?
答案 0 :(得分:2)
由于交易所背后的一些复杂性和限制,这是您通常希望来自第三方的一些帮助之一。
有些插件可用于使用Flash实现此目的的jquery,但我更喜欢避免使用Flash的任何内容(个人偏好)。查看Valums Ajax-Upload我现在在我的生产网站上使用了大约一年,它运行良好且易于使用。我最喜欢的插件是它对大多数网站的设计和布局都不显眼。
此外,这是一个常见问题。看看这些答案......
答案 1 :(得分:0)
杰夫,
我会像这样编码...
如果你想在代码中显示自己,你必须调用微调器。如果要显示图像。
答案 2 :(得分:0)
您可以使用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;
}