asp插入图片的一种模式

 

这个上传图片到数据库的模式,是弹出图片添加对话框,request只用于提交图片对象,不提交其他参数,省去分析提交的其他数据项,其他数据项通过session提交。

  图片单独一个表,由id和内容组成,id自动编号。图片和对象关系做一个表,关键字自动编号,一个记录存储图片id、对象id、和图片类型。对象id对应其他的表中的数据,图片类型可作扩展需要,比如根据不同要求显示不同的样式。
  首先程序提交图片选择页面(img_insert.asp),通过imagetype变量,转入不同的对象提交页面(如img_bd.asp),提交完成后返回的页面、对象id及图片类型等参数,调用插入函数(uploadimg),
将图片插入image表后,再将图片id,对象id,图片类型插入relation_image表,然后转入返回的页面(img_back.asp),返回图片id等参数。返回页面在规定时间内自动关闭,回到完成后返回的页面。

<a href="img_insert.asp?imagetype=2&mainid=<%=对象id%>"  target="_blank">添加图片</a>

img_insert.asp文件代码,可以生成上传前的预览。

功能还可以根据具体情况进行扩展,如添加检查文件类型的判断程序

<html xmlns="http://www.w3.org/1999/xhtml">
<!--#include file="../connect/connect.asp"-->
<head>
<script type="text/javascript">

function showimg(){  //在网页“showpic”控件中生成图片预览
if (document.picForm.picture.value!=""){
  document.all("showpic").src=document.picForm.picture.value;
 }
}
function uploadImg(imgtype) { 
  if(document.picForm.picture.value==""){
  alert("请点击“浏览”选择文件");
  return;  }

if(document.all("showpic").fileSize>(300*1024)){ //检查文件大小是否超限
    alert("选择的文件超过300K,请重新选择");
   return;
   } 

showTipInfo("正在上传图片");
 if (imgtype==1) {
 document.picForm.action =  "img_bd.asp"; 
 }
 else if (imgtype==2){
 document.picForm.action =  "其他页面"; 
 }
 else {
 document.picForm.action = "../public/error.asp?strerror=有错误"; 
 }
 
 //document.picForm.target = "iframe_upload";
 document.picForm.submit();
 //}
}
function showTipInfo(text, tipBox) {
 
 var body = document.body;
 if (!tipBox) {
  var tipBox = document.createElement("div");
  body.appendChild(tipBox);
 }
 tipBox.innerHTML = text;
 tipBox.id = "tipBoxDiv";
 tipBox.style.color = "#333";
 tipBox.style.border = "2px solid #cecece";
 tipBox.style.background = "#ffffe1";
 tipBox.style.padding = "10px";
 tipBox.style.display = "block";
 tipBox.style.zIndex = "1";
 tipBox.style.position = "absolute";
 var x = (body.offsetWidth - tipBox.offsetWidth)/2;
 var y = Math.ceil((document.documentElement.clientHeight - tipBox.offsetHeight)/2) + document.documentElement.scrollTop;
 tipBox.style.left = x + "px";
 tipBox.style.top = y + "px";
}
function onCancel(){cancel();}

// -->
</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<link rel="stylesheet" type="text/css" href="../style/popwindows.css" />

<title>添加/修改图片</title>
<%dim imgtype
if request("imagetype")<>"" then
  imgtype=request("imagetype")
  session("curmainid")=request("mainid")
else
  imgtype=0
  session("curmainid")=""
end if
%>
 </head>
<body οnlοad="window.resizeTo(400,300)">

<div align="left">
<form enctype="multipart/form-data"  method="post" name="picForm">
请选择要上传的文件: <br/>

 <div id="imgBox_1">
<input name="picture"  type="file" class="text" style="width: 90%;" title="“浏览”选择本地图片" οnchange="showimg();" /><br>

<div>图片文件最大为300k<br />
</div>
     
 <div id="artItem-button">
  <input type="button" name="ok" value="确 定" class="button-submit" id="imgBut_1" οnclick="uploadImg(<%=imgtype%>);" />
  <input type="button" name="cancel" value="取 消" class="button2" οnclick="return onCancel();" />
    </div>
 </div></form>

<img  width="250" height="150" name="showpic"/>
</div>


  </body>
  </html>

img_bd.asp文件代码
<!--#include file="../connect/connect.asp"-->数据库连接文件
<!--#include file="img_upimg.asp"-->上传图片函数
<%
call uploadimg("pmdj2.asp",session("curmainid"),1)
%>
<!--#include file="../connect/closecon.asp"-->


img_upimg.asp文件代码
<%
'web 插入图片后回到的页面,mainid 图片对应对象的id ,imgtype 图片类型

sub uploadimg(web,mainid,imgtype)
  dim FormSize
  dim FormData
  dim bnCRLF
  dim Divider
  dim DataStart
  dim DataEnd
  dim strlen
  dim img
  dim imageid
  FormSize=Request.TotalBytes
  FormData=Request.BinaryRead(FormSize)
  bnCRLF=chrB(13) & chrB(10)
  Divider=LEFTB(FormData,INSTRB(FormData,bnCRLF)-1)

  DataStart=INSTRB(FormData,bnCRLF & bnCRLF)+4
  DataEnd=INSTRB(DataStart+1,FormData,divider)-DataStart
  img=MIDB(FormData,DataStart,DataEnd)
  strlen=len(img)
  if strlen<155000 then
   dim resbuff
   set resbuff=server.CreateObject("adodb.recordset")
    resbuff.open "SELECT * FROM image where image_id is null",conn,1,2
    if err.number <> 0 then
    response.write "数据库操作失败:"&err.description
    response.redirect"error.htm"
    else  
       resbuff.addnew
   resbuff("p_i_content").appendchunk img
    imageid=resbuff("p_i_id")
   resbuff.update  '
    end if
    resbuff.close
    set resbuff=nothing
 conn.execute "insert into relation_image(r_i_imageid,r_i_objectid,r_i_type) values("& imageid &",'"& mainid &"',"& imgtype &")"
 response.Redirect("img_back.asp?imageid="&imageid &"&imgtype="& imgtype &"&web=" & web)
  else
 Response.write("图片不能大于300k!")
     Response.write("<A href='"& web &"' >返回</a>")
  end if
end sub
%>

img_back.asp文件内容,把图片id和图片类型传回

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>图片添加完成</title>
<script language="JavaScript">
 function closeit(id)
{ //setTimeout("self.close()",10000) //毫秒
 // document.imagefrom.action = "pmdj2.asp";
 //document.imagefrom
 document.imagefrom.submit();
 setTimeout("self.close()",500);
 //window.close
}
 </script>
</head>

<body οnlοad="closeit()"  >
<form name="imagefrom" action = "<%=request("web")%>"  target="mainFrame" >
<input name="imageid" type="hidden" value="<%=request("imageid")%>">
<input name="imagetype" type="hidden" value="<%=request("imagetype")%>">

</form>
</body>
</html>

 

 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET MVC框架提供了多种方法来实现一次上传多张图片的功能。 一种常见的方法是在前端使用HTML5的文件选择控件来实现多文件上传。在HTML页面中,我们可以使用<input type="file" multiple/>来创建一个文件上传控件,multiple属性表示可以选择多个文件。当用户选择了多张图片后,这些图片会被封装成一个FileList对象,我们可以通过JavaScript将FileList对象传递给后端。 在后端,我们可以使用ASP.NET MVC的控制器来处理这些上传的图片。在控制器的方法中,我们可以通过参数绑定来接收上传的文件。可以使用HttpPostedFileBase类型的数组来接收多个文件,并通过循环遍历每个文件进行处理。对于每个文件,我们可以使用其FileName属性获取文件名,使用SaveAs方法保存文件到服务器指定的路径。 另一种方法是使用第三方插件或库来简化多文件上传的实现。例如,可以使用FineUploader、Dropzone.js等插件来实现多文件上传的功能。这些插件提供了专门的JavaScript API和样式来处理文件上传操作,并且可以与ASP.NET MVC框架无缝集成。 总体而言,实现ASP.NET MVC一次上传多张图片的功能可以通过HTML5的文件选择控件及其API,或使用第三方插件来实现。无论使用哪种方法,我们都需要在前端进行多文件选择和上传操作,并在后端控制器中接收和处理上传的文件。当然,为了确保上传的文件不会超过服务器的负载能力,我们还可以在前端和后端进行一些限制和校验,比如限制上传文件的数量和大小。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值