前端上传控件
① Bootstrap-fileinput上传控件 前端JS怎么获取服务器端执行后的返回值,求解
$("#file1").on("fileuploaded", function (event, data, previewId, index) {
va url = data.response.Result;
});
② 前端上传文件的几种方法
1.表单上传
最传统的图片上传方式是form表单上传,使用form表单的input[type=”file”]控件,打开系统的文件选择对话框,从而达到选择文件并上传的目的。
form表单上传
表单上传需要注意以下几点:
(1).提供form表单,method必须是post。
(2).form表单的enctype必须是multipart/form-data。
javascript学习交流群:453833554
enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码。默认地,表单数据会编码为 "application/x-www-form-urlencoded"。就是说,在发送到服务器之前,所有字符都会进行编码。HTML表单如何打包数据文件是由enctype这个属性决定的。enctype有以下几种取值:
application/x-www-form-urlencoded:在发送前编码所有字符(默认)(空格被编码为’+’,特殊字符被编码为ASCII十六进制字符)。
multipart/form-data:不对字符编码。在使用包含文件上传控件的表单时,必须使用该值。
text/plain:空格转换为 “+” 加号,但不对特殊字符编码。
默认enctype=application/x-www-form-urlencoded,所以表单的内容会按URL规则编码,然后根据表单的提交方法:
method=’get’ 编码后的表单内容附加在请求连接后,
method=’post’ 编码后的表单内容作为post请求的正文内容。
③ web前端上传图片的几种方法
下面给你介绍3种web前端上传图片的方法:
1.表单上传
最传统的图片上传方式是form表单上传,使用form表单的input[type=”file”]控件,打开系统的文件选择对话框,从而达到选择文件并上传的目的。
ajax无刷新上传
Ajax无刷新上传的方式,本质上与表单上传无异,只是把表单里的内容提出来采用ajax提交,并且由前端决定请求结果回传后的展示结果。
3.各类插件上传
当上传的需求要求可预览、显示上传进度、中断上传过程、大文件分片上传等等,这时传统的表单上传很难实现这些功能,我们可以借助现有插件完成。
如网络上传插件Web Uploader、jQuery图片预览插件imgPreview 、拖拽上传与图像预览插件Dropzone.js等等,大家可根据项目实际需求选择适合的插件。
④ ext js 浏览控件(上传图片功能)
用js或者提交后后台检测fileExt。
js:
<script language="JavaScript" type="text/JavaScript">
var img=null;
function checkPic(Form1){
var location=Form1.pic.value;
if(location==""){
alert("友情提示:\n\n请先选择图片文件,然后再点击“上传照片”按钮。");
window.location.href=window.location.href;
return false;
}
var point = location.lastIndexOf(".");
var type = location.substr(point);
if(type==".jpg"||type==".gif"||type==".png"||type==".JPG"||type==".GIF"||type==".PNG"){
img=document.createElement("img");
img.style.position="absolute";
img.style.visibility="hidden";
document.body.insertAdjacentElement("beforeend",img);
img.src=location;
//if(img.fileSize>35540)
if(img.fileSize>102400) {
alert("友情提示:\n\n您上传的图片尺寸超过了网站的限定,请不要大于102400字节,宽度小于450px。\n\n您目前上传的图片宽度:"+img.offsetWidth+"px,高度:"+img.offsetHeight+"px,图片大小:"+img.fileSize+"字节。\n\n请调整到规定大小再上传!谢谢合作!");
window.location.href=window.location.href;
return false;
}else
return true;
}
else{
alert("友情提示:您要上传的图片格式不对!\n\n只能输入jpg、png或者gif格式的图片,\n\n请重新选择图片!谢谢合作!");
window.location.href=window.location.href;
return false;
}
return true;
}
function changesrc(){
yourpic.src=Form1.pic.value;
}
</script>
后台:
<%
set upload=new upload_file
if upload.form("act")="uploadfile" then
filepath=trim(upload.form("filepath"))
filelx=trim(upload.form("filelx"))
i=0
for each formName in upload.File
set file=upload.File(formName)
fileExt=lcase(file.FileExt) '得到的文件扩展名不含有.
if file.filesize<100 then
response.write "<span style=""font-family: 宋体; font-size: 9pt"">请先选择你要上传的文件![ <a href=# onclick=history.go(-1)>重新上传</a> ]</span>"
response.end
end if
if (filelx<>"swf") and (filelx<>"jpg") then
'response.write "<span style=""font-family: 宋体; font-size: 9pt"">该文件类型不能上传![ <a href=# onclick=history.go(-1)>重新上传</a> ]</span>"
'response.end
response.write "<script language='javascript'>" & VbCRlf
response.write "alert('友情提示:\n\n您上传出错啦!该文件类型不能上传!\n\n请重新选择文件再上传,请点击“确定”重新输入!');" & VbCrlf
response.write "history.go(-1);" & vbCrlf
response.write "</script>" & VbCRLF
response.end
end if
if filelx="swf" then
if fileext<>"swf" then
'response.write "<span style=""font-family: 宋体; font-size: 9pt"">只能上传swf格式的Flash文件![ <a href=# onclick=history.go(-1)>重新上传</a> ]</span>"
'response.end
response.write "<script language='javascript'>" & VbCRlf
response.write "alert('友情提示:\n\n您上传出错啦!只能上传swf格式的Flash文件!\n\n请重新选择文件再上传,请点击“确定”重新输入!');" & VbCrlf
response.write "history.go(-1);" & vbCrlf
response.write "</script>" & VbCRLF
response.end
end if
end if
if filelx="jpg" then
if fileext<>"gif" and fileext<>"jpg" and fileext<>"png" then
'response.write "<span style=""font-family: 宋体; font-size: 9pt"">只能上传jpg或gif格式的图片![ <a href=# onclick=history.go(-1)>重新上传</a> ]</span>"
'response.end
response.write "<script language='javascript'>" & VbCRlf
response.write "alert('友情提示:\n\n您上传出错啦!只能上传jpg、png或gif格式的图片!\n\n请重新选择文件再上传,请点击“确定”重新输入!');" & VbCrlf
response.write "history.go(-1);" & vbCrlf
response.write "</script>" & VbCRLF
response.end
end if
end if
if filelx="swf" then
if file.filesize>(3000*1024) then
'response.write "<span style=""font-family: 宋体; font-size: 9pt"">最大只能上传 3M 的Flash文件![ <a href=# onclick=history.go(-1)>重新上传</a> ]</span>"
'response.end
response.write "<script language='javascript'>" & VbCRlf
response.write "alert('友情提示:\n\n您上传出错啦!最大只能上传 300k 的Flash文件!\n\n请重新选择文件再上传,请点击“确定”重新输入!');" & VbCrlf
response.write "history.go(-1);" & vbCrlf
response.write "</script>" & VbCRLF
response.end
end if
end if
if filelx="jpg" then
if file.filesize>(200*724) then
'response.write "<span style=""font-family: 宋体; font-size: 9pt"">最大只能上传 1000K 的图片文件![ <a href=# onclick=history.go(-1)>重新上传</a> ]</span>"
'response.end
response.write "<script language='javascript'>" & VbCRlf
response.write "alert('友情提示:\n\n您上传的图片尺寸超过了网站的限定,最大只能上传 600K 的图片文件!\n\n请将图片调整到宽度为:450px,小于规定大小再上传!\n\n请点击“确定”重新输入!');" & VbCrlf
response.write "history.go(-1);" & vbCrlf
response.write "</script>" & VbCRLF
response.end
end if
end if
%>
要注意与上传控件一起配合好才行的。 有问题找偶。
⑤ 怎样用JS控制文件上传时FILE控件内默认的文件类型
有两种方式,
1、用fileupload控件在后台判断
FileUpload1.FileContent.Length判断大小,
System.IO.Path.GetExtension(FileUpload1.FileName)获取文件后缀
2、用jquery上传控件,可以直接判断类型和大小
另外js也可以在前台判断文件类型,但不能获取文件大小,需要引用插件