当前位置:首页 » 文件管理 » djangoajax上传文件

djangoajax上传文件

发布时间: 2023-05-08 09:48:17

Ⅰ 怎样利用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" +

Ⅱ 怎么用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上传图片至服务器(服务器就是本地文件夹),并将图片地址存入MySQL数据库方便以后调用

1. 后台程序编写上传、录入数据库的逻辑代码;
2. 通过ajax访问逻辑代码地址上传

Ⅳ ajax上传文件提交时,enctype=multipart/form-data怎么带过去

form中的字段,加上get set方法

private FormFile file;

private String filename;

private String filesize;

action 部分:

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception {

String dir="D:/";

UpFileForm uff=(UpFileForm)form;

FormFile file=uff.getFile();

if(file.getFileSize()==0){

return mapping.findForward("success");

}

String fname=file.getFileName();

String size=Integer.toString(file.getFileSize())+"bytes";

InputStream streamIn=file.getInputStream();

OutputStream streamOut=new FileOutputStream(dir+"/"+fname);

int bytesRead=0;

byte[] buffer=new byte[8192];

while((bytesRead=streamIn.read(buffer,0,8192))!=-1){

streamOut.write(buffer,0,bytesRead);

}

streamOut.close();

streamIn.close();

uff.setFilename(fname);

uff.setFilesize(size);

file.destroy();

return mapping.findForward("success");

}

这样将上传的文件存在d盘。

Ⅳ 怎么用ajax提交file文件上传

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

Ⅵ python django通过ajax向后端传json怎么解析

这么有逼格的问题竟然没分!

$.ajax({
url:"xxx",
dataType:'json',
data:formData,
type:"POST",
cache:false,
processData:false,
contentType:false
})
.done(function(data){
varcode=data.err_code;
if(code.length!==0){
switch(code){
case'10001':
layer.msg(loginTimeoutText);
self.locatToLogin();
break;
case'液皮10003':
猛笑layer.msg(systemBusyText);
break;
case'0':
location.href="/xxx/?id="+id+"&vote_id="+data.id;
break;
case'9999':
layer.msg(notLoginedInText);
//self.locatToLogin();
break;
default:
break;
枝埋含}
}
})
.fail(function(){
layer.msg(failText);
});

Ⅶ ajax如何 实现 文件上传



程序说明

使用说明

实例化时,第一个必要参数是file控件对象:

newQuickUpload(file);


第二个可选参数用来设置系统的默认属性,包括
属性: 默认值//说明
parameter:{},//参数对象
action:"",//设置action
timeout:0,//设置超时(秒为单位)
onReady:function(){},//上传准备时执行
onFinish:function(){},//上传完成时执行
onStop:function(){},//上传停止时执行
onTimeout:function(){}//上传超时时执行

还提供了以下方法:
upload:执行上传操作;
stop:停止上传操作;
dispose:销毁程序。

varQuickUpload=function(file,options){

this.file=$$(file);

this._sending=false;//是否正在上传
this._timer=null;//定时器
this._iframe=null;//iframe对象
this._form=null;//form对象
this._inputs={};//input对象
this._fFINISH=null;//完成执行函数

$$.extend(this,this._setOptions(options));
};
QuickUpload._counter=1;
QuickUpload.prototype={
//设置默认属性
_setOptions:function(options){
this.options={//默认值
action:"",//设置action
timeout:0,//设置超时(秒为单位)
parameter:{},//参数对象
onReady:function(){},//上传准备时执行
onFinish:function(){},//上传完成时执行
onStop:function(){},//上传停止时执行
onTimeout:function(){}//上传超时时执行
};
return$$.extend(this.options,options||{});
},
//上传文件
upload:function(){
//停止上一次上传
this.stop();
//没有文件返回
if(!this.file||!this.file.value)return;
//可能在onReady中修改相关属性所以放前面
this.onReady();
//设置iframe,form和表单控件
this._setIframe();
this._setForm();
this._setInput();
//设置超时
if(this.timeout>0){
this._timer=setTimeout($$F.bind(this._timeout,this),this.timeout*1000);
}
//开始上传
this._form.submit();
this._sending=true;
},
//设置iframe
_setIframe:function(){
if(!this._iframe){
//创建iframe
variframename="QUICKUPLOAD_"+QuickUpload._counter++,
iframe=document.createElement($$B.ie?"<iframename=""+iframename+"">":"iframe");
iframe.name=iframename;
iframe.style.display="none";
//记录完成程序方便移除
varfinish=this._fFINISH=$$F.bind(this._finish,this);
//iframe加载完后执行完成程序
if($$B.ie){
iframe.attachEvent("onload",finish);
}else{
iframe.onload=$$B.opera?function(){this.onload=finish;}:finish;
}
//插入body
varbody=document.body;body.insertBefore(iframe,body.childNodes[0]);

this._iframe=iframe;
}
},
//设置form
_setForm:function(){
if(!this._form){
varform=document.createElement('form'),file=this.file;
//设置属性
$$.extend(form,{
target:this._iframe.name,method:"post",encoding:"multipart/form-data"
});
//设置样式
$$D.setStyle(form,{
padding:0,margin:0,border:0,
backgroundColor:"transparent",display:"inline"
});
//提交前去掉form
file.form&&$$E.addEvent(file.form,"submit",$$F.bind(this.dispose,this));
//插入form
file.parentNode.insertBefore(form,file).appendChild(file);

this._form=form;
}
//action可能会修改
this._form.action=this.action;
},
//设置input
_setInput:function(){
varform=this._form,oldInputs=this._inputs,newInputs={},name;
//设置input
for(nameinthis.parameter){
varinput=form[name];
if(!input){
//如果没有对应input新建一个
input=document.createElement("input");
input.name=name;input.type="hidden";
form.appendChild(input);
}
input.value=this.parameter[name];
//记录当前input
newInputs[name]=input;
//删除已有记录
deleteoldInputs[name];
}
//移除无用input
for(nameinoldInputs){form.removeChild(oldInputs[name]);}
//保存当前input
this._inputs=newInputs;
},
//停止上传
stop:function(){
if(this._sending){
this._sending=false;
clearTimeout(this._timer);
//重置iframe
if($$B.opera){//opera通过设置src会有问题
this._removeIframe();
}else{
this._iframe.src="";
}
this.onStop();
}
},
//销毁程序
dispose:function(){
this._sending=false;
clearTimeout(this._timer);
//清除iframe
if($$B.firefox){
setTimeout($$F.bind(this._removeIframe,this),0);
}else{
this._removeIframe();
}
//清除form
this._removeForm();
//清除dom关联
this._inputs=this._fFINISH=this.file=null;
},
//清除iframe
_removeIframe:function(){
if(this._iframe){
variframe=this._iframe;
$$B.ie?iframe.detachEvent("onload",this._fFINISH):(iframe.onload=null);
document.body.removeChild(iframe);this._iframe=null;
}
},
//清除form
_removeForm:function(){
if(this._form){
varform=this._form,parent=form.parentNode;
if(parent){
parent.insertBefore(this.file,form);parent.removeChild(form);
}
this._form=this._inputs=null;
}
},
//超时函数
_timeout:function(){
if(this._sending){this._sending=false;this.stop();this.onTimeout();}
},
//完成函数
_finish:function(){
if(this._sending){this._sending=false;this.onFinish(this._iframe);}
}
}

Ⅷ ajax的res怎么传到其他文件

传其他参竖坦数
ajax文件上传怎么传带纤腊其他参数,Ajax进行文件与其他参数的上传功能

光启元
转载
关注
0点赞·945人阅读
记得前一段时间,为了研究Ajax文件上传,找了很多资料,在网上看到的大部分是form表单的方式提交文件,对于Ajax方式提交文件并且也要提交表单中其他数据,发现提及的并不是很多,后来在同事的帮助下,使用ajaxfileupload最终完成了文件上传与其他提交的操作,现在分享给大家,希望大家能有有所帮助。本文主要介绍了使用Ajax进行文件与其他参数的上传功能(java开发),非常不错,具有参考借鉴价值,需要的朋友参考下吧,希望能帮助到大家。

文件上传:

操作步骤:

1 导入jar包:

我们在使用文件上传时,需要使用到两个jar包,分别是commons-io与commons-fileupload,在这里我使用的两个版本分别是2.4与1.3.1版本的,需要使用JS文件与jar包最后会发给大家一个连接(如何失效请直接我给留言,我会及时更改,谢谢)。

2 修改配置文件:

当我们导入的jar包是不够的,我们需要使用到这些jar包,由于我当时使用的是SSM框架,所以我是在application-content.xml中配置一下CommonsMultipartResolver,具体配置方法如下:

104857600

4096

3 JSP文件:

大家对form表单提交问价的方式很熟悉,但是我们有很多情况下并不能直接使用form表单方式直接提交。这时候我们就需要使用Ajax方式提交,Ajax有很多的好处,比如当我们不需要刷新页面获希望进行局部蠢滑刷新的时候,我们就可以使用Ajax。

Ⅸ Django上传原理求解

随着网站运作,难免有些时候需要上传文件。上传文件自然是上传到网站所在的服务器,日积月累,慢慢地网站存储空间越来越少。而且网站迁移和备份都不方便,使用这些资源时也占用大量服务器流量。

较好的解决方案是使用第三方存储服务器,例如七牛、阿里云OSS、亚马逊S3等。将文件都放到这些存储服务器,可以减少服务器负担。服务器只剩下必要的静态文件和代码。


以阿里云OSS为例,讲解如何使用第三方存储服务器。(刚好最近用到这个,而且Django有其他人写好的第三方库)

首先,需要拥有OSS。这个去阿里云购买即可。购买之后可得到密钥等一系列信息。

接着,安装oss2库,该库是Python对应oss的操作库。


这样设置,点击文件链接,即可下载并且下载文件名是上传的文件名。若你不是什么类型文件都需要这么处理,可以判断filename的后缀名加以处理。

Ⅹ django文件上传的时候怎么能加一个上传进度的显示

首先需要一个表单来让用户选择要上传的文件。

1 <form id="form_upload" action="/upload" method="POST">
2 <input type="file" name="picture" id="picture" />
3 <input type="hidden" id="X-Progress-ID" name="X-Progress-ID" value=""/>
4 <input type="hidden" id="id" name="id" value=""/>
5 <input id="form_submit_button" class="tp-button" type="submit" value="Submit" />
6 </form>
这里增加了两个隐藏的输入框,第一个是 ‘X-Progress-ID’,代表上传 ID,这样我们才能够在服务器端支持并发的上传请求。稍后我们会看到,服务器是如何处理这个值的。

然后还有一个隐藏输入框 ‘id’,在我们的例子里代表菜品的编号。

我们将使用 Ajax 来发送 POST 请求,这样表单便可以很好地集成在现代的网络界面中,同时包含一个进度条。我们打算使用 jQuery Form plugin 来实现这一点。

函数 ajaxSubmit() 将会帮我们搞定一切。

为上传 ID 生成一个随机字串,并用它设置输入框的值。
需要指定一个用于上传请求的 URL 和两个回调函数:一个在请求前调用,另一个在请求完成后调用。

1 $('#X-Progress-ID').val('random string');
2 var options = {
3 dataType: 'xml',
4 url: '/upload?X-Progress-ID='+$('#X-Progress-ID').val(),
5 beforeSubmit: showRequest,
6 success: showResponse
7 }
8 $('#form_upload').ajaxSubmit(options);
showRequest 回调函数只需要像下面这么简单就行了:

1 function showRequest(formData, jqForm, options) {
2 // do something with formData
3 return True;
4 }
在 showResponse 函数中,我们需要处理响应,并对它进行操作。在我的例子里,我处理了服务器返回的带有状态值的 xml。

1 function showResponse(response) {
2 // do something with response
3 }
用户按下提交的时候,我们希望显示一个进度条,因此可以使用下面的 JS 代码,向表单添加进度条。progressBar() 方法是 jQuery progress bar plugin 的一部分。

1 $('#form_upload').find('#form_submit_input').append('<span id="uploadprogressbar"></span<');
2 $('#form_upload').find('#uploadprogressbar').progressBar();
现在我们需要添加一个每隔几秒运行一次的函数,来从服务器获取上传进度,并相应地更新进度条。

为此,我们使用 setInterval() 向服务器发出一个 GET 请求,获取 JSON 格式的进度值。我们向服务器传送上传 ID。当返回 null 值的时候,就可以知道上传已经结束。

01 function startProgressBarUpdate(upload_id) {
02 $("#uploadprogressbar").fadeIn();
03 if(g_progress_intv != 0)
04 clearInterval(g_progress_intv);
05 g_progress_intv = setInterval(function() {
06 $.getJSON("/get_upload_progress?X-Progress-ID="
07 + upload_id, function(data) {
08 if (data == null) {
09 $("#uploadprogressbar").progressBar(100);
10 clearInterval(g_progress_intv);
11 g_progress_intv = 0;
12 return;
13 }
14 var percentage = Math.floor(100 * parseInt(data.uploaded) / parseInt(data.length));
15 $("#uploadprogressbar").progressBar(percentage);
16 });

热点内容
高级语言都有与之对应的编译程序或解释程序 发布:2025-02-13 00:54:46 浏览:570
塞班java 发布:2025-02-13 00:48:27 浏览:157
java策略设计模式 发布:2025-02-13 00:43:35 浏览:867
c语言二码表 发布:2025-02-13 00:37:46 浏览:235
免费加密文件 发布:2025-02-13 00:35:00 浏览:176
菲亚特菲翔怎么区别配置 发布:2025-02-13 00:21:19 浏览:985
服务器好坏重点看什么 发布:2025-02-13 00:19:47 浏览:587
php把数据插入数据库 发布:2025-02-13 00:09:48 浏览:369
eclipse查看jar包源码 发布:2025-02-12 23:59:35 浏览:973
电脑主机服务器维修 发布:2025-02-12 23:59:26 浏览:302