当前位置:首页 » 文件传输 » ajax上传文件
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

ajax上传文件

发布时间: 2022-01-16 22:55:49

‘壹’ 怎样利用ajax实现多个文件上传

<script type="text/javascript">
$(document).ready(function () {

$("#uploadify").uploadify({
'uploader': 'image/uploadify.swf', //uploadify.swf文件的相对路径,该swf文件是一个带有文字BROWSE的按钮,点击后淡出打开文件对话框
'script': 'Handler1.ashx',// script : 后台处理程序的相对路径
'cancelImg': 'image/cancel.png',
'buttenText': '请选择文件',//浏览按钮的文本,默认值:BROWSE。
'sizeLimit':999999999,//文件大小显示
'floder': 'Uploader',//上传文件存放的目录
'queueID': 'fileQueue',//文件队列的ID,该ID与存放文件队列的div的ID一致
'queueSizeLimit': 120,//上传文件个数限制
'progressData': 'speed',//上传速度显示
'auto': false,//是否自动上传
'multi': true,//是否多文件上传
//'onSelect': function (e, queueId, fileObj) {
// alert("唯一标识:" + queueId + "\r\n" +
// "文件名:" + fileObj.name + "\r\n" +

‘贰’ 怎么样通过jQuery Ajax实现上传文件

Query Ajax在web应用开发中很常用,它主要包括有ajax,get,post,load,getscript等等这几种常用无刷新操作方法,接下来通过本文给大家介绍jquery ajax 上传文件处理方式。
FormData对象
XMLHttpRequest Level 2添加了一个新的接口FormData.利用FormData对象,我们可以通过JavaScript用一些键值对来模拟一系列表单控件,我们还可以使用XMLHttpRequest的send()方法来异步的提交这个”表单”.比起普通的ajax,使用FormData的最大优点就是我们可以异步上传一个二进制文件.
所有主流浏览器的较新版本都已经支持这个对象了,比如Chrome 7+、Firefox 4+、IE 10+、Opera 12+、Safari 5+。之前都是用原生js的XMLHttpRequest写的请求
XMLHttpRequest方式
xhr.open("POST", uri, true);

xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
// Handle response.
alert(xhr.responseText); // handle response.
}
};
fd.append('myFile', file);
// Initiate a multipart/form-data upload
xhr.send(fd);

其实jquery的ajax也可以支持到的,关键是设置:processData 和 contentType 。
ajax方式

var formData = new FormData();
var name = $("input").val();
formData.append("file",$("#upload")[0].files[0]);
formData.append("name",name);
$.ajax({
url : Url,
type : 'POST',
data : formData,
// 告诉jQuery不要去处理发送的数据
processData : false,
// 告诉jQuery不要去设置Content-Type请求头
contentType : false,
beforeSend:function(){
console.log("正在进行,请稍候");
},
success : function(responseStr) {
if(responseStr.status===0){
console.log("成功"+responseStr);
}else{
console.log("失败");
}
},
error : function(responseStr) {
console.log("error");
}
});

‘叁’ Jquery/ajax 如何以put方式上传文件

using System.Data.OleDb;
using System.Data;

namespace _211
{
/// <summary>
/// DBQuery 的摘要说明。
/// </summary>
public sealed class DBQuery
{
static OleDbConnection conn=DBConnection.getConn();
static DataTable dt;

public static DataTable OpenQuery(string sql)
{
OleDbDataAdapter da=new OleDbDataAdapter(sql,conn);
dt=new DataTable();
try
{
da.Fill(dt);
}
catch(Exception ex)
{
throw ex;
}
finally
{
da.Dispose();
}

return dt;
}

public static DataTable OpenTable(string TableName)
{
OleDbDataAdapter da=new OleDbDataAdapter("select * from "+TableName,conn);
dt=new DataTable();
try
{
da.Fill(dt);
}
catch(Exception ex)
{
throw ex;
}
finally
{
da.Dispose();
}

return dt;
}

public static DataTable OpenTable(string TableName,int count)
{
OleDbDataAdapter da=new OleDbDataAdapter("select top "+count.ToString()+" * from "+TableName,conn);
dt=new DataTable();
try
{
da.Fill(dt);
}
catch(Exception ex)
{
throw ex;
}
finally
{
da.Dispose();
}

return dt;
}

public static DataTable OpenTable(string TableName,string IndexColumn,bool IsAsc)
{
OleDbDataAdapter da=new OleDbDataAdapter("select * from "+TableName+" order by "+IndexColumn+" "+(IsAsc?"Asc":"Desc"),conn);
dt=new DataTable();
try
{
da.Fill(dt);
}
catch(Exception ex)
{
throw ex;
}
finally
{
da.Dispose();
}

return dt;
}

public static DataTable OpenTable(string TableName,int count,string IndexColumn,bool IsAsc)
{
OleDbDataAdapter da=new OleDbDataAdapter("select top "+count.ToString()+" * from "+TableName+" order by "+IndexColumn+" "+(IsAsc?"Asc":"Desc"),conn);
dt=new DataTable();
try
{
da.Fill(dt);
}
catch(Exception ex)
{
throw ex;
}
finally
{
da.Dispose();
}

return dt;
}

public static DataTable OpenTable(string TableName,string[] column,bool IsCondition)
{
string tempsql="";
for(int i=0;i<column.Length;i++)
{
tempsql+=column[i];
if(i<column.Length-1)
{
if(IsCondition)
tempsql+=" and ";
else
tempsql+=",";
}
}

;
string sql="";
if(IsCondition)
sql="select * from "+TableName+" where "+tempsql;
else
sql="select "+tempsql+" from "+TableName;

OleDbDataAdapter da=new OleDbDataAdapter(sql,conn);
dt=new DataTable();
try
{
da.Fill(dt);
}
catch(Exception ex)
{
throw ex;
}
finally
{
da.Dispose();
}

return dt;
}

public static DataTable OpenTable(string TableName,string[] column,string[] condition)
{
string tempsql="";
string tempsql2="";

for(int i=0;i<column.Length;i++)
{
tempsql+=column[i];
if(i<column.Length-1)tempsql+=",";
}

for(int i=0;i<condition.Length;i++)
{
tempsql2+=condition[i];
if(i<condition.Length-1)tempsql2+=" and ";
}

OleDbDataAdapter da=new OleDbDataAdapter("select "+tempsql+" from "+TableName+" where "+tempsql2,conn);
dt=new DataTable();
try
{
da.Fill(dt);
}
catch(Exception ex)
{
throw ex;
}
finally
{
da.Dispose();
}

return dt;
}

public static DataTable OpenTable(string TableName,int count,string[] column)
{
string tempsql="";
for(int i=0;i<column.Length;i++)
{
tempsql+=column[i];
if(i<column.Length-1)tempsql+=",";
}

OleDbDataAdapter da=new OleDbDataAdapter("select top "+count.ToString()+" "+tempsql+" from "+TableName,conn);
dt=new DataTable();
try
{
da.Fill(dt);
}
catch(Exception ex)
{
throw ex;
}
finally
{
da.Dispose();
}

return dt;
}

public static DataTable OpenTable(string TableName,int count,string[] column,string[] condition)
{
string tempsql="";
string tempsql2="";

for(int i=0;i<column.Length;i++)
{
tempsql+=column[i];
if(i<column.Length-1)tempsql+=",";
}

for(int i=0;i<condition.Length;i++)
{
tempsql2+=condition[i];
if(i<condition.Length-1)tempsql2+=" and ";
}

OleDbDataAdapter da=new OleDbDataAdapter("select top "+count.ToString()+" "+tempsql+" from "+TableName+" where "+tempsql2,conn);
dt=new DataTable();
try
{
da.Fill(dt);
}
catch(Exception ex)
{
throw ex;
}
finally
{
da.Dispose();
}

return dt;
}

public static DataTable OpenTable(string TableName,int count,string[] column,string IndexColumn,bool IsAsc)
{
string tempsql="";
for(int i=0;i<column.Length;i++)
{
tempsql+=column[i];
if(i<column.Length-1)tempsql+=",";
}

OleDbDataAdapter da=new OleDbDataAdapter("select top "+count.ToString()+" "+tempsql+" from "+TableName+" order by "+IndexColumn+" "+(IsAsc?"Asc":"Desc"),conn);
dt=new DataTable();
try
{
da.Fill(dt);
}
catch(Exception ex)
{
throw ex;
}
finally
{
da.Dispose();
}

return dt;
}

public static DataTable OpenTable(string TableName,int count,string[] column,string[] condition,string IndexColumn,bool IsAsc)
{
string tempsql="";
string tempsql2="";

for(int i=0;i<column.Length;i++)
{
tempsql+=column[i];
if(i<column.Length-1)tempsql+=",";
}

for(int i=0;i<condition.Length;i++)
{
tempsql2+=condition[i];
if(i<condition.Length-1)tempsql2+=" and ";
}

OleDbDataAdapter da=new OleDbDataAdapter("select top "+count.ToString()+" "+tempsql+" from "+TableName+" where "+tempsql2+" order by "+IndexColumn+" "+(IsAsc?"Asc":"Desc"),conn);
dt=new DataTable();
try
{
da.Fill(dt);
}
catch(Exception ex)
{
throw ex;
}
finally
{
da.Dispose();
}

return dt;
}

public static DataTable OpenTable(string TableName,string[] column,string IndexColumn,bool IsAsc,int PageSize,int Page)
{
string tempsql="";
for(int i=0;i<column.Length;i++)
{
tempsql+=column[i];
if(i<column.Length-1)tempsql+=",";
}

string sql="";
if(Page==0)
sql="select top "+PageSize.ToString()+" "+tempsql+" from "+TableName+" order by "+IndexColumn+" "+(IsAsc?"Asc":"Desc");
else
sql="select top "+PageSize.ToString()+" "+tempsql+" from "+TableName+" where "+IndexColumn+(IsAsc?">":"<")+"(select "+(IsAsc?"max":"min")+"("+IndexColumn+") from "+TableName+" where "+IndexColumn+" in (select top "+(Page*PageSize)+" "+IndexColumn+" from "+TableName+" order by "+IndexColumn+" "+(IsAsc?"Asc":"Desc")+")) order by "+IndexColumn+" "+(IsAsc?"Asc":"Desc");

OleDbDataAdapter da=new OleDbDataAdapter(sql,conn);

dt=new DataTable();
try
{
da.Fill(dt);
}
catch(Exception ex)
{
throw ex;
}
finally
{
da.Dispose();
}

return dt;
}

public static DataTable OpenTable(string TableName,string[] column,string[] condition,string IndexColumn,bool IsAsc,int PageSize,int Page)
{
string tempsql="";
string tempsql2="";

for(int i=0;i<column.Length;i++)
{
tempsql+=column[i];
if(i<column.Length-1)tempsql+=",";
}

for(int i=0;i<condition.Length;i++)
{
tempsql2+=condition[i];
if(i<condition.Length-1)tempsql2+=" and ";
}

‘肆’ 怎么用ajax实现上传文件的功能

HTTP File Server

http-file-server是用 python 实现的 HTTP 文件服务器,支持上传和下载文件。

运行

$ python file-server.py files 8001

其中第一个参数files是存放文件的路径,第二个参数8001是 HTTP 服务器端口。

接口

1. 读取文件

GET /pathtofile/filename

2. 读取文件夹下所有文件(已经忽略隐藏文件)

GET /path

返回文件列表为JSON数组,文件名末尾带有/的表示是文件夹。filename为文件名,mtime为修改时间。

[{"filename":"f1.txt","mtime":1001},{"filename":"p3/","mtime":1002}]

3. 上传文件

采用POST方式上传文件,URL参数中传参数name表示上传的文件名,POST内容为文件内容。

POST /upload?name=filename

ajax示例:

// file is a FileReader object
var data = file.readAsArrayBuffer();
var xhr = new XMLHttpRequest();
var url = "http://localhost:8001/upload?name=xxx.md";
xhr.open("post", url, true);
xhr.setRequestHeader("Accept", "application/json, text/javascript, */*; q=0.01");
xhr.onreadystatechange = function() {
if (xhr.readyState==4 && xhr.status==200)
{
console.log(xhr.responseText);
}
}
xhr.send(data);

文件名 filename 可以包含相对路径。比如:upload?name=md/xxx.md。则上传至md目录下。

‘伍’ 如何用ajax上传文件

  1. 引入ajaxfileupload.js

jQuery.extend({


createUploadIframe:function(id,uri)
{
//createframe
varframeId='jUploadFrame'+id;

if(window.ActiveXObject){
vario=document.createElement('<iframeid="'+frameId+'"name="'+frameId+'"/>');
if(typeofuri=='boolean'){
io.src='javascript:false';
}
elseif(typeofuri=='string'){
io.src=uri;
}
}
else{
vario=document.createElement('iframe');
io.id=frameId;
io.name=frameId;
}
io.style.position='absolute';
io.style.top='-1000px';
io.style.left='-1000px';

document.body.appendChild(io);

returnio
},
createUploadForm:function(id,fileElementId)
{
//createform
varformId='jUploadForm'+id;
varfileId='jUploadFile'+id;
varform=$('<formaction=""method="POST"name="'+formId+'"id="'+formId+'"enctype="multipart/form-data"></form>');
varoldElement=$('#'+fileElementId);
varnewElement=$(oldElement).clone();
$(oldElement).attr('id',fileId);
$(oldElement).before(newElement);
$(oldElement).appendTo(form);
//setattributes
$(form).css('position','absolute');
$(form).css('top','-1200px');
$(form).css('left','-1200px');
$(form).appendTo('body');
returnform;
},
addOtherRequestsToForm:function(form,data)
{
//addextraparameter
varoriginalElement=$('<inputtype="hidden"name=""value="">');
for(varkeyindata){
name=key;
value=data[key];
varcloneElement=originalElement.clone();
cloneElement.attr({'name':name,'value':value});
$(cloneElement).appendTo(form);
}
returnform;
},

ajaxFileUpload:function(s){
//TODOintroceglobalsettings,,notonlytimeout
s=jQuery.extend({},jQuery.ajaxSettings,s);
varid=newDate().getTime()
varform=jQuery.createUploadForm(id,s.fileElementId);
if(s.data)form=jQuery.addOtherRequestsToForm(form,s.data);
vario=jQuery.createUploadIframe(id,s.secureuri);
varframeId='jUploadFrame'+id;
varformId='jUploadForm'+id;
//Watchforanewsetofrequests
if(s.global&&!jQuery.active++)
{
jQuery.event.trigger("ajaxStart");
}
varrequestDone=false;
//Createtherequestobject
varxml={}
if(s.global)
jQuery.event.trigger("ajaxSend",[xml,s]);
//Waitforaresponsetocomeback
varuploadCallback=function(isTimeout)
{
vario=document.getElementById(frameId);
try
{
if(io.contentWindow)
{
xml.responseText=io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
xml.responseXML=io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;

}elseif(io.contentDocument)
{
xml.responseText=io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
xml.responseXML=io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
}
}catch(e)
{
jQuery.handleError(s,xml,null,e);
}
if(xml||isTimeout=="timeout")
{
requestDone=true;
varstatus;
try{
status=isTimeout!="timeout"?"success":"error";
//
if(status!="error")
{
//processthedata()
vardata=jQuery.uploadHttpData(xml,s.dataType);
//Ifalocalcallbackwasspecified,fireitandpassitthedata
if(s.success)
s.success(data,status);

//Firetheglobalcallback
if(s.global)
jQuery.event.trigger("ajaxSuccess",[xml,s]);
}else
jQuery.handleError(s,xml,status);
}catch(e)
{
status="error";
jQuery.handleError(s,xml,status,e);
}

//Therequestwascompleted
if(s.global)
jQuery.event.trigger("ajaxComplete",[xml,s]);

//HandletheglobalAJAXcounter
if(s.global&&!--jQuery.active)
jQuery.event.trigger("ajaxStop");

//Processresult
if(s.complete)
s.complete(xml,status);

jQuery(io).unbind()

setTimeout(function()
{ try
{
$(io).remove();
$(form).remove();

}catch(e)
{
jQuery.handleError(s,xml,null,e);
}

},100)

xml=null

}
}
//Timeoutchecker
if(s.timeout>0)
{
setTimeout(function(){
//
if(!requestDone)uploadCallback("timeout");
},s.timeout);
}
try
{
//vario=$('#'+frameId);
varform=$('#'+formId);
$(form).attr('action',s.url);
$(form).attr('method','POST');
$(form).attr('target',frameId);
if(form.encoding)
{
form.encoding='multipart/form-data';
}
else
{
form.enctype='multipart/form-data';
}
$(form).submit();

}catch(e)
{
jQuery.handleError(s,xml,null,e);
}
if(window.attachEvent){
document.getElementById(frameId).attachEvent('onload',uploadCallback);
}
else{
document.getElementById(frameId).addEventListener('load',uploadCallback,false);
}
return{abort:function(){}};

},

uploadHttpData:function(r,type){
vardata=!type;
data=type=="xml"||data?r.responseXML:r.responseText;
//Ifthetypeis"script",evalitinglobalcontext
if(type=="script")
jQuery.globalEval(data);
//GettheJavaScriptobject,ifJSONisused.
if(type=="json")
{
//,
//youhavetodeletethe'<pre></pre>'tag.
//ThepretaginChromehasattribute,sohavetouseregextoremove
vardata=r.responseText;
varrx=newRegExp("<pre.*?>(.*?)</pre>","i");
varam=rx.exec(data);
//thisisthedesireddataextracted
vardata=(am)?am[1]:"";//theonlysubmatchorempty
eval("data="+data);
}
//evaluatescriptswithinhtml
if(type=="html")
jQuery("<div>").html(data).evalScripts();
//alert($('param',data).each(function(){alert($(this).attr('value'));}));
returndata;
}
})

2.引入上传文件所需的jar

7.获取之后怎么处理自己看着办咯,我只能帮到这里了

‘陆’ 怎么用ajax上传文件

表单提交啊<input type="file" />

‘柒’ ajax+html实现文件上传有哪几种方法

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>大文件切割上传</title>
<link rel="stylesheet" href="">
<script>
function selfile(){
const LENGTH = 1024 * 1024 * 10;//每次上传的大小
var file = document.getElementsByName('video')[0].files[0];//文件对象
var filename=document.getElementsByName('video')[0].files[0].name;
var totalSize = file.size;//文件总大小
var start = 0;//每次上传的开始字节
var end = start + LENGTH;//每次上传的结尾字节
var fd = null//创建表单数据对象
var blob = null;//二进制对象
var xhr = null;//xhr对象
while(start < totalSize){
fd = new FormData();//每一次需要重新创建
xhr = new XMLHttpRequest();//需要每次创建并设置参数
xhr.open('POST','upload.php',false);
blob = file.slice(start,end);//根据长度截取每次需要上传的数据
fd.append('video',blob);//添加数据到fd对象中
fd.append('filename',filename); //获取文件的名称
xhr.send(fd);//将fd数据上传

//重新设置开始和结尾
start = end;
end = start + LENGTH;

}

}
</script>
</head>
<body>
<h1>大文件切割上传</h1>
<input type="file" name="video" onchange="selfile();" />
</body>
</html>

‘捌’ java+AJAX上传文件

http://download.csdn.net/source/214291

http://www.winu.cn/htmls/170/111/

‘玖’ 怎么用ajax提交file文件上传

上传的文件是没有办法和表单内容一起异步的,可考虑使用jquery的ajaxfileupload,或是其他的插件,异步上传文件后,然后再对表单进行操作。

‘拾’ JS怎样用AJAX上传文件

可以使用jquery的uploadfild插件,或者jquery upload插件进行附件上传,两种插件的实现原理差不多,可以进行参考。