如何使用javascript发送文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21985856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send file using javascript
提问by b22
I am trying to make an upload page which takes file to upload. I am using Spring framework here my query is on Upload button I am calling a JavaScript method which should send my file to controller using jQuery AJAX. Is there any way to pass this through JavaScript?
我正在尝试制作一个需要上传文件的上传页面。我在这里使用 Spring 框架,我的查询位于上传按钮上,我正在调用一个 JavaScript 方法,该方法应该使用 jQuery AJAX 将我的文件发送到控制器。有没有办法通过 JavaScript 传递它?
Following is the code which I am trying.
以下是我正在尝试的代码。
<body>
<div style="text-align: center; margin-top: 60px;">
<form enctype="multipart/form-data">
Select file:
<input type="file" name="dataFile" id="fileAttachment"/><br/><br/>
<div style="text-align: center; margin-top: 100px;">
<input style="cursor: pointer;" onmouseover="" onclick="uploadAttachment()" class="dialogbox" type="submit" value="Upload Report" />
</div>
</form>
</div>
</body>
JS:
JS:
<script language="Javascript">
function uploadAttachment(){
var Name = jQuery('#Name option:selected').text();
jQuery.post('upload',{Name:Name}
function(data){
if(data==1)
alert("success");
else
alert("failed");
});
}
</script>
on controller.java page following is the code written
在controller.java页面下面是写的代码
@RequestMapping(value = "upload", method=RequestMethod.POST)
public @ResponseBody String upload(HttpServletRequest request, HttpServletResponse response,
@RequestParam("Name") String Name){
System.out.println(Name);
}
回答by Jwalin Shah
If you are in fact seeking a pure javascript way to upload an image/file, then the following tutorial from 2009 will tell you exactly how to do that:
如果您实际上正在寻找一种纯粹的 javascript 方式来上传图像/文件,那么以下 2009 年的教程将告诉您如何做到这一点:
http://igstan.ro/posts/2009-01-11-ajax-file-upload-with-pure-javascript.html
http://igstan.ro/posts/2009-01-11-ajax-file-upload-with-pure-javascript.html
I ran into this when I wanted to add basic-authentication to a form submission, without enabling cookies. E.g. when you have username/password fields with your filename, file, etc fields.
当我想向表单提交添加基本身份验证而不启用 cookie 时,我遇到了这个问题。例如,当您有包含文件名、文件等字段的用户名/密码字段时。
Hope this helps!
希望这可以帮助!
回答by Inus Saha
I think you can try the following code to upload file with javascript.
我认为您可以尝试使用以下代码使用 javascript 上传文件。
function uploadAttachment(){
$('#fileAttachment').trigger('click');
if (typeof timer != 'undefined') {
clearInterval(timer);
}
timer = setInterval(function() {
if ($('#fileAttachment').val() != '') {
clearInterval(timer);
$.ajax({
url: 'YOUR_UPLOAD_URL',
type: 'post',
dataType: 'json',
data: new FormData($('#fileAttachment').closest('form')[0]),
cache: false,
contentType: false,
processData: false,
success: function(response){
}
});
}
}, 500);
}
You need to replace YOUR_UPLOAD_URL
with your server upload URL.
您需要替换YOUR_UPLOAD_URL
为您的服务器上传 URL。