asp+layui image upload, return thumbnail, return upload progress

The project needs an asp+layui image upload, return the thumbnail, and return the upload progress. After searching the Internet for a long time, I can't find a free one. A certain fairy charges 100 yuan. Isn't it good to add a chicken leg for 100 yuan? Free for small partners who need it We~, renderings:

<!--#include virtual="Upload.asp"-->

<%
dim upload,file,formName,SavePath,filename
dim strMsg

dim http_ref : http_ref = Request.ServerVariables("QUERY_STRING")

SavePath = GetKey(http_ref,"SavePath=","&")
ifsrc = GetKey(http_ref,"ifsrc=","&")


set upload=new upload_file    '建立上传对象
for each formName in upload.file '列出所有上传了的文件

	set file=upload.file(formName)  '生成一个文件对象
	if file.ErrCode>0 then
		strMsg=strMsg & "上传失败!原因:" & file.ErrMsg & vbcrlf
	else
		randomize
		sTime=now
        if ifsrc="0" then
		        filename=year(sTime)&right("0" & month(sTime),2)&right("0" & day(sTime),2)&right("0" & hour(sTime),2)&right("0" & minute(sTime),2)&right("0" & second(sTime),2) & cstr(int(900*rnd)+100) & "."&file.FileExt
		        file.SaveToFile Server.mappath(SavePath & filename) '保存文件到服务器
        else
		        filename=SavePath & year(sTime)&right("0" & month(sTime),2)&right("0" & day(sTime),2)&right("0" & hour(sTime),2)&right("0" & minute(sTime),2)&right("0" & second(sTime),2) & cstr(int(900*rnd)+100) & "."&file.FileExt
		        file.SaveToFile Server.mappath(filename) '保存文件到服务器
        End if
		strMsg=strMsg & filename &  vbcrlf
        fileTitle=file.FileName
	end if
	set file=nothing
next
if strMsg<>"" then 
    strMsg=left(strMsg,len(strMsg)-2)
    response.write "{""code"": 0,""msg"": ""上传成功"",""data"": {""src"": """&strMsg&""",""SavePath"": """&SavePath&""",""title"": """&fileTitle&"""}} "
else
    response.write "{""code"": 1,""msg"": ""上传失败""} "
end if
set upload=nothing
%>
<script>
layui.use(['upload', 'element', 'layer'], function(){
var $ = layui.jquery
,upload = layui.upload
,element = layui.element
,layer = layui.layer;

//常规使用 - 普通图片上传
var uploadInst = upload.render({
elem: '#upload20'
,url: 'Return.asp?SavePath=/Uploads/images/&ifsrc=0' //改成您自己的上传接口
,before: function(obj){
//预读本地文件示例,不支持ie8
obj.preview(function(index, file, result){
$('#demo20').attr('src', result); //图片链接(base64)
});

element.progress('upload', '0%'); //进度条复位
layer.msg('上传中', {icon: 16, time: 0});
}
,done: function(res){
//如果上传失败
if(res.code > 0){
return layer.msg('上传失败');
}
//上传成功
  var uploadurl1 = $('#uploadurl20')
  uploadurl1.attr("value",res.data.src);
  uploadurl1.attr("title",res.data.SavePath);
  uploadurl1.hidden();
//上传成功的一些操作
//……
$('#demoText20').html(''); //置空上传失败的状态
}
,error: function(){
//演示失败状态,并实现重传
var demoText = $('#demoText20');
demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
demoText.find('.demo-reload').on('click', function(){
uploadInst.upload();
});
}
//进度条
,progress: function(n, elem, e){
element.progress('upload20', n + '%'); //可配合 layui 进度条元素使用
if(n == 100){
layer.msg('上传完毕', {icon: 1});
}
}
});

});
</script>

Complete code package download http://www.024000.cn/asp_layui.zip

Guess you like

Origin blog.csdn.net/oZhongRan/article/details/118097152