Jquery grails:不刷新页面的图像上传
我在stack和Google上做了一些关于通过Ajax上传图像的研究(在Grails中),发现了一些旧链接,因此我决定问你们“有什么方法可以使用Ajax上传我的用户图像吗?”?我发现了一个Grails插件,但该插件的文档还不够。我想使用该插件(正如我所说的,由于缺乏该插件的良好文档,我很难实现它),或者请告诉我一些可选选项(如果可用)。试试这个 查看:Jquery grails:不刷新页面的图像上传,jquery,ajax,grails,image-uploading,grails-plugin,Jquery,Ajax,Grails,Image Uploading,Grails Plugin,我在stack和Google上做了一些关于通过Ajax上传图像的研究(在Grails中),发现了一些旧链接,因此我决定问你们“有什么方法可以使用Ajax上传我的用户图像吗?”?我发现了一个Grails插件,但该插件的文档还不够。我想使用该插件(正如我所说的,由于缺乏该插件的良好文档,我很难实现它),或者请告诉我一些可选选项(如果可用)。试试这个 查看: <html> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.
<html>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script language="Javascript">
function fileUpload(form, action_url, div_id) {
// Create the iframe...
var iframe = document.createElement("iframe");
iframe.setAttribute("id", "upload_iframe");
iframe.setAttribute("name", "upload_iframe");
iframe.setAttribute("width", "0");
iframe.setAttribute("height", "0");
iframe.setAttribute("border", "0");
iframe.setAttribute("style", "width: 0; height: 0; border: none;");
// Add to document...
form.parentNode.appendChild(iframe);
window.frames['upload_iframe'].name = "upload_iframe";
iframeId = document.getElementById("upload_iframe");
// Add event...
var eventHandler = function () {
if (iframeId.detachEvent) iframeId.detachEvent("onload", eventHandler);
else iframeId.removeEventListener("load", eventHandler, false);
// Message from server...
if (iframeId.contentDocument) {
content = iframeId.contentDocument.body.innerHTML;
} else if (iframeId.contentWindow) {
content = iframeId.contentWindow.document.body.innerHTML;
} else if (iframeId.document) {
content = iframeId.document.body.innerHTML;
}
document.getElementById(div_id).innerHTML = content;
// Del the iframe...
setTimeout('iframeId.parentNode.removeChild(iframeId)', 250);
}
if (iframeId.addEventListener) iframeId.addEventListener("load", eventHandler, true);
if (iframeId.attachEvent) iframeId.attachEvent("onload", eventHandler);
// Set properties of form...
form.setAttribute("target", "upload_iframe");
form.setAttribute("action", action_url);
form.setAttribute("method", "post");
form.setAttribute("enctype", "multipart/form-data");
form.setAttribute("encoding", "multipart/form-data");
// Submit the form...
form.submit();
document.getElementById(div_id).innerHTML = "Uploading...";
}
</script>
<body>
<g:form>
<input type="file" name="myFile"/><br/><br/>
<input type="button" value="upload" onClick="fileUpload(this.form, '${g.createLink(controller: 'dashboard', action: 'test')}', 'upload'); return false;">
<div id="upload"></div>
</g:form>
</body>
</html>
def test() {
if (params.myFile) {
def fileName
def inputStream
if (params.myFile instanceof CommonsMultipartFile) {
fileName = params.myFile?.originalFilename
inputStream = params.myFile.getInputStream()
} else {
fileName = params.myFile
inputStream = request.getInputStream()
}
fileName = fileName.replaceAll(" ", "_")
File storedFile = new File("DIRECTORY_PATH_TO_SAVE_IMAGE/${fileName}")
storedFile.append(inputStream)
render '<img src="data:' + 'jpg' + ';base64,' + new String(new Base64().encode(storedFile.bytes), "UTF-8") + '" ' + ' />'
} else {
render "No Image"
}
}
函数文件上传(表单、操作url、分区id){
//创建iframe。。。
var iframe=document.createElement(“iframe”);
setAttribute(“id”,“upload_iframe”);
setAttribute(“名称”、“上传iframe”);
iframe.setAttribute(“宽度”、“0”);
iframe.setAttribute(“高度”、“0”);
iframe.setAttribute(“边框”、“0”);
setAttribute(“样式”,“宽度:0;高度:0;边框:无;”;
//添加到文档。。。
form.parentNode.appendChild(iframe);
window.frames['upload\u iframe'].name=“upload\u iframe”;
iframeId=document.getElementById(“上传iframe”);
//添加事件。。。
var eventHandler=函数(){
if(iframeId.detachEvent)iframeId.detachEvent(“onload”,eventHandler);
else iframeId.removeEventListener(“加载”,eventHandler,false);
//来自服务器的消息。。。
if(iframeId.contentDocument){
content=iframeId.contentDocument.body.innerHTML;
}else if(iframeId.contentWindow){
content=iframeId.contentWindow.document.body.innerHTML;
}else if(iframeId.document){
content=iframeId.document.body.innerHTML;
}
getElementById(div_id).innerHTML=content;
//删除iframe。。。
setTimeout('iframeId.parentNode.removeChild(iframeId)'250);
}
if(iframeId.addEventListener)iframeId.addEventListener(“加载”,eventHandler,true);
if(iframeId.attachEvent)iframeId.attachEvent(“onload”,eventHandler);
//设置窗体的属性。。。
form.setAttribute(“target”、“upload_iframe”);
form.setAttribute(“action”,action\u url);
form.setAttribute(“方法”、“帖子”);
setAttribute(“enctype”、“多部分/表单数据”);
setAttribute(“编码”、“多部分/表单数据”);
//提交表格。。。
表单提交();
document.getElementById(div_id).innerHTML=“上传…”;
}
行动:
<html>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script language="Javascript">
function fileUpload(form, action_url, div_id) {
// Create the iframe...
var iframe = document.createElement("iframe");
iframe.setAttribute("id", "upload_iframe");
iframe.setAttribute("name", "upload_iframe");
iframe.setAttribute("width", "0");
iframe.setAttribute("height", "0");
iframe.setAttribute("border", "0");
iframe.setAttribute("style", "width: 0; height: 0; border: none;");
// Add to document...
form.parentNode.appendChild(iframe);
window.frames['upload_iframe'].name = "upload_iframe";
iframeId = document.getElementById("upload_iframe");
// Add event...
var eventHandler = function () {
if (iframeId.detachEvent) iframeId.detachEvent("onload", eventHandler);
else iframeId.removeEventListener("load", eventHandler, false);
// Message from server...
if (iframeId.contentDocument) {
content = iframeId.contentDocument.body.innerHTML;
} else if (iframeId.contentWindow) {
content = iframeId.contentWindow.document.body.innerHTML;
} else if (iframeId.document) {
content = iframeId.document.body.innerHTML;
}
document.getElementById(div_id).innerHTML = content;
// Del the iframe...
setTimeout('iframeId.parentNode.removeChild(iframeId)', 250);
}
if (iframeId.addEventListener) iframeId.addEventListener("load", eventHandler, true);
if (iframeId.attachEvent) iframeId.attachEvent("onload", eventHandler);
// Set properties of form...
form.setAttribute("target", "upload_iframe");
form.setAttribute("action", action_url);
form.setAttribute("method", "post");
form.setAttribute("enctype", "multipart/form-data");
form.setAttribute("encoding", "multipart/form-data");
// Submit the form...
form.submit();
document.getElementById(div_id).innerHTML = "Uploading...";
}
</script>
<body>
<g:form>
<input type="file" name="myFile"/><br/><br/>
<input type="button" value="upload" onClick="fileUpload(this.form, '${g.createLink(controller: 'dashboard', action: 'test')}', 'upload'); return false;">
<div id="upload"></div>
</g:form>
</body>
</html>
def test() {
if (params.myFile) {
def fileName
def inputStream
if (params.myFile instanceof CommonsMultipartFile) {
fileName = params.myFile?.originalFilename
inputStream = params.myFile.getInputStream()
} else {
fileName = params.myFile
inputStream = request.getInputStream()
}
fileName = fileName.replaceAll(" ", "_")
File storedFile = new File("DIRECTORY_PATH_TO_SAVE_IMAGE/${fileName}")
storedFile.append(inputStream)
render '<img src="data:' + 'jpg' + ';base64,' + new String(new Base64().encode(storedFile.bytes), "UTF-8") + '" ' + ' />'
} else {
render "No Image"
}
}
def测试(){
if(params.myFile){
def文件名
def输入流
if(params.myFile instanceof commonmultipartfile){
fileName=params.myFile?.originalFilename
inputStream=params.myFile.getInputStream()
}否则{
fileName=params.myFile
inputStream=request.getInputStream()
}
fileName=fileName.replaceAll(“,”)
File storedFile=新文件(“目录\路径\保存\图像/${fileName}”)
storedFile.append(inputStream)
渲染“”
}否则{
渲染“无图像”
}
}
Ajax不支持二进制数据(文件上传)。您找到的任何插件都可能使用Flash或隐藏iFrame。这两种都是可以接受的解决方案。@Gregg您对ajax/xhr的了解相当陈旧。通过ajax/xhr上传文件已经有很多年了。@RayNicholus你能给我一个链接让我读一下吗?你只能通过带有IE10+的ajax(当然还有真正的浏览器)来完成。这不是绝大多数人的解决方案。