Javascript 使用Ajax上传文件?
我正在一个网页上工作,我正在使用dialogbox的即兴Api,其中包括html表单:Javascript 使用Ajax上传文件?,javascript,jquery,ajax,impromptu,Javascript,Jquery,Ajax,Impromptu,我正在一个网页上工作,我正在使用dialogbox的即兴Api,其中包括html表单: html = '<label>Name <input type="text" id="fname" name="fname" value="'+STUDENT_NAME+'"></label><br />' + '
html = '<label>Name <input type="text" id="fname" name="fname" value="'+STUDENT_NAME+'"></label><br />'
+ '<label>Sex<select id="sex"> <option value="m" >Male</option> <option value="f" selected>Female</option>'
+ '</select>'
+ '</label><br />'
+ '<label>Category<select id="cat"> <option value="a">A</option> <option value="b" selected>B</option>'
+ '</select>' + '</label>'+
'<input type="file" id="logo">'+
'<br />';
html='Name
'
+“性别男性女性”
+ ''
+“
”
+“A类B类”
+ '' + ''+
''+
“
”;
使用Ajax提交表单,如下所示:
$.ajax({
url : 'AddStudent.jsp',
data : 'classId=' +
<%=class_id%>
+ '&name='
+ document.getElementById('fname').value
+ '&cat='
+ document.getElementById('cat').value
+ '&func=del' + '&sex='
+ document.getElementById('sex').value,
type : 'post',
success : function(msg) {
//events}
String saveFile = "";
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1, contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
saveFile = "D:/SERVERE/" + saveFile;
File ff = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
}
$.ajax({
url:'AddStudent.jsp',
数据:“classId=”+
+“&name=”
+document.getElementById('fname').value
+“&cat=”
+document.getElementById('cat').value
+'&func=del'+'&sex='1〕
+document.getElementById('sex')。值,
键入:“post”,
成功:功能(msg){
//事件}
我已获得上传文件的代码,如下所示:
$.ajax({
url : 'AddStudent.jsp',
data : 'classId=' +
<%=class_id%>
+ '&name='
+ document.getElementById('fname').value
+ '&cat='
+ document.getElementById('cat').value
+ '&func=del' + '&sex='
+ document.getElementById('sex').value,
type : 'post',
success : function(msg) {
//events}
String saveFile = "";
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1, contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
saveFile = "D:/SERVERE/" + saveFile;
File ff = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
}
String saveFile=”“;
String contentType=request.getContentType();
if((contentType!=null)&&(contentType.indexOf(“多部分/表单数据”)>=0)){
DataInputStream in=新的DataInputStream(request.getInputStream());
int formDataLength=request.getContentLength();
字节数据字节[]=新字节[formDataLength];
int byteRead=0;
int totalBytesRead=0;
while(totalBytesRead
但这不适用于Ajax方法。我希望文件必须上载到同一页面上。如何使用Ajax或其他技术实现这一点?如果可以使用HTML5文件API和FormData对象,请提供帮助
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress',function(ev){
console.log((ev.loaded/ev.total)+'%');
}, false);
xhr.onreadystatechange = function(ev){
// Blah blah blah, you know how to make AJAX requests
};
xhr.open('POST', url, true);
var files = document.getElementById('logo').files;
var data = new FormData();
for(var i = 0; i < files.length; i++) data.append('file'+i, files[i]);
xhr.send(data);
var xhr=new-XMLHttpRequest();
xhr.upload.addEventListener('progress',函数(ev){
console.log((ev.loaded/ev.total)+'%');
},假);
xhr.onreadystatechange=函数(ev){
//诸如此类,你知道如何发出AJAX请求
};
xhr.open('POST',url,true);
var files=document.getElementById('logo').files;
var data=new FormData();
对于(var i=0;i
您可以执行data.append('name',document.getElementById('name')等操作来追加所有其他表单字段