欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net文件上传解决方案(图片上传、单文件上传、多文件上传、检查文件类型)

程序员文章站 2024-02-17 10:06:40
小编之前也介绍了许多asp.net文件上传的解决案例,今天来个asp.net文件上传大集合。 1 使用标准html来进行图片上传 前台代码:

小编之前也介绍了许多asp.net文件上传的解决案例,今天来个asp.net文件上传大集合。

1 使用标准html来进行图片上传
前台代码:

<body> 
 <form id="form1" runat="server"> 
 <div> 
  <table> 
   <tr> 
    <td colspan="2" style="height: 21px" > 
     使用标准html来进行图片上传</td> 
   </tr> 
   <tr> 
    <td style="width: 400px"> 
     <input id="inputfile" style="width: 399px" type="file" runat="server" /></td> 
    <td style="width: 80px"> 
     <asp:button id="uploadbutton" runat="server" text="上传图片" onclick="uploadbutton_click" /></td> 
   </tr> 
   <tr> 
    <td colspan="2" > 
     <asp:label id="lb_info" runat="server" forecolor="red"></asp:label></td>     
   </tr> 
  </table>  
 </div> 
 </form> 
</body>


后台代码:

using system; 
using system.data; 
using system.configuration; 
using system.web; 
using system.web.security; 
using system.web.ui; 
using system.web.ui.webcontrols; 
using system.web.ui.webcontrols.webparts; 
using system.web.ui.htmlcontrols; 

public partial class _default : system.web.ui.page 
{ 
 protected void page_load(object sender, eventargs e) 
 { 

 } 
 protected void uploadbutton_click(object sender, eventargs e) 
 { 
  string uploadname = inputfile.value;//获取待上传图片的完整路径,包括文件名 
  //string uploadname = inputfile.postedfile.filename; 
  string picturename = "";//上传后的图片名,以当前时间为文件名,确保文件名没有重复 
  if (inputfile.value != "") 
  { 
   int idx = uploadname.lastindexof("."); 
   string suffix = uploadname.substring(idx);//获得上传的图片的后缀名 
   picturename = datetime.now.ticks.tostring() + suffix; 
  } 
  try 
  { 
   if (uploadname != "") 
   { 
    string path = server.mappath("~/images/"); 
    inputfile.postedfile.saveas(path + picturename); 
   } 
  } 
  catch (exception ex) 
  { 
   response.write(ex); 
  } 
 } 
}

2 单文件上传
这是最基本的文件上传,在asp.net1.x中没有这个fileupload控件,只有html的上传控件,那时候要把html控件转化为服务器控件, 很不好用。其实所有文件上传的美丽效果都是从这个fileupload控件衍生,第一个例子虽然简单却是根本。
前台代码:

<body> 
 <form id="form1" runat="server"> 
 <div> 
  <table style="width: 90%"> 
   <tr> 
    <td style="width: 159px" colspan=2> 
     <strong><span style="font-size: 10pt">最简单的单文件上传</span></strong></td> 
   </tr> 
   <tr> 
    <td style="width: 600px"> 
     <asp:fileupload id="fileupload1" runat="server" width="600px" /></td> 
    <td align=left> 
     <asp:button id="fileupload_button" runat="server" text="上传图片" onclick="fileupload_button_click" /></td> 
   </tr> 
   <tr> 
    <td colspan=2> 
     <asp:label id="upload_info" runat="server" forecolor="red" width="767px"></asp:label></td> 
   </tr> 
  </table>  
 </div> 
 </form> 
</body>

后台代码:

using system; 
using system.data; 
using system.configuration; 
using system.web; 
using system.web.security; 
using system.web.ui; 
using system.web.ui.webcontrols; 
using system.web.ui.webcontrols.webparts; 
using system.web.ui.htmlcontrols; 

public partial class _default : system.web.ui.page 
{ 
 protected void page_load(object sender, eventargs e) 
 { 

 } 
 protected void fileupload_button_click(object sender, eventargs e) 
 { 
  try 
  { 
   if (fileupload1.postedfile.filename == "") 
   //if (fileupload1.filename == "") 
   //if (!fileupload1.hasfile)  //获取一个值,该值指示 system.web.ui.webcontrols.fileupload 控件是否包含文件。包含文件,则为 true;否则为 false。 
   { 
    this.upload_info.text = "请选择上传文件!"; 
   } 
   else 
   { 
    string filepath = fileupload1.postedfile.filename; //得到的是文件的完整路径,包括文件名,如:c:\documents and settings\administrator\my documents\my pictures\20022775_m.jpg 
    //string filepath = fileupload1.filename;    //得到上传的文件名20022775_m.jpg 
    string filename = filepath.substring(filepath.lastindexof("\\") + 1);//20022775_m.jpg 
    string serverpath = server.mappath("~/images/") + filename;//取得文件在服务器上保存的位置c:\inetpub\wwwroot\website1\images\20022775_m.jpg 
    fileupload1.postedfile.saveas(serverpath);//将上传的文件另存为 
    this.upload_info.text = "上传成功!"; 
   } 
  } 
  catch (exception ex) 
  { 
   this.upload_info.text = "上传发生错误!原因是:" + ex.tostring(); 
  } 
 } 
}


3、多文件上传
前台代码:

<body> 
 <form id="form1" runat="server"> 
 <div> 
 <table style="width: 343px"> 
   <tr> 
    <td style="width: 100px"> 
     多文件上传</td> 
    <td style="width: 100px"> 
    </td> 
   </tr> 
   <tr> 
    <td style="width: 100px"> 
     <asp:fileupload id="fileupload1" runat="server" width="475px" /> 
     </td> 
    <td style="width: 100px"> 
     </td> 
   </tr> 
   <tr> 
    <td style="width: 100px"> 
     <asp:fileupload id="fileupload2" runat="server" width="475px" /></td> 
    <td style="width: 100px"> 
    </td> 
   </tr> 
   <tr> 
    <td style="width: 100px"> 
     <asp:fileupload id="fileupload3" runat="server" width="475px" /></td> 
    <td style="width: 100px"> 
    </td> 
   </tr> 
   <tr> 
    <td style="width: 100px"> 
     <asp:button id="bt_upload" runat="server" onclick="bt_upload_click" text="一起上传" /> 
     <asp:label id="lb_info" runat="server" forecolor="red" width="448px"></asp:label></td> 
    <td style="width: 100px"> 
    </td> 
   </tr> 
  </table> 
 </div> 
 </form> 
</body>


后台代码:

using system; 
using system.data; 
using system.configuration; 
using system.web; 
using system.web.security; 
using system.web.ui; 
using system.web.ui.webcontrols; 
using system.web.ui.webcontrols.webparts; 
using system.web.ui.htmlcontrols; 

public partial class _default : system.web.ui.page 
{ 
 protected void page_load(object sender, eventargs e) 
 { 

 } 
 protected void bt_upload_click(object sender, eventargs e) 
 { 
  if (fileupload1.postedfile.filename == "" && fileupload2.postedfile.filename == "" && fileupload3.postedfile.filename == "") 
  { 
   this.lb_info.text = "请选择文件!"; 
  } 
  else 
  { 
   httpfilecollection myfiles = request.files; 
   for (int i = 0; i < myfiles.count; i++) 
   { 
    httppostedfile mypost = myfiles[i]; 
    try 
    { 
     if (mypost.contentlength > 0) 
     { 
      string filepath = mypost.filename;//c:\documents and settings\administrator\my documents\my pictures\20022775_m.jpg 
      string filename = filepath.substring(filepath.lastindexof("\\") + 1);//20022775_m.jpg 
      string serverpath = server.mappath("~/images/") + filename;//c:\inetpub\wwwroot\website2\images\20022775_m.jpg 
      mypost.saveas(serverpath); 
      this.lb_info.text = "上传成功!"; 
     } 
    } 
    catch (exception ex) 
    { 
     this.lb_info.text = "上传发生错误!原因:" + ex.message.tostring(); 
    } 
   } 
  } 
 } 
}

4、客户端检查上传文件类型(以上传图片为例)

<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %> 

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
 <title>客户端检查上传文件类型</title> 
 <script language="javascript"> 
 function check_filetype() 
 { 
  var str=document.getelementbyid("fileupload1").value; 
  var pos=str.lastindexof("."); 
  var lastname=str.substring(pos,str.length); 
  if(lastname.tolowercase()!=".jpg"&&lastname.tolowercase()!=".gif") 
  { 
   alert("您上传的文件类型为"+lastname+",图片必须为.jpg,.gif类型"); 
   return false; 
  } 
  else 
  { 
   return true; 
  }   
 } 
 </script> 
</head> 
<body> 
 <form id="form1" runat="server"> 
 <div> 
  <table> 
   <tr> 
    <td colspan="2"> 
     客户端检查上传文件类型</td>     
   </tr> 
   <tr> 
    <td style="width: 444px"> 
     <asp:fileupload id="fileupload1" runat="server" width="432px" /></td> 
    <td style="width: 80px"> 
     <asp:button id="bt_upload" runat="server" text="上传图片" onclick="bt_upload_click" onclientclick="return check_filetype()" /></td> 
   </tr> 
   <tr> 
    <td colspan="2" style="height: 21px"> 
     <asp:label id="lb_info" runat="server" forecolor="red" width="515px"></asp:label></td>     
   </tr> 
  </table>  
 </div> 
 </form> 
</body> 
</html>

注意:点击上传时先触发客户端事件onclientclick="return check_filetype()"

using system; 
using system.data; 
using system.configuration; 
using system.web; 
using system.web.security; 
using system.web.ui; 
using system.web.ui.webcontrols; 
using system.web.ui.webcontrols.webparts; 
using system.web.ui.htmlcontrols; 

public partial class _default : system.web.ui.page 
{ 
 protected void page_load(object sender, eventargs e) 
 { 

 } 

 protected void bt_upload_click(object sender, eventargs e) 
 { 
  try 
  { 
   if (fileupload1.postedfile.filename == "") 
   { 
    this.lb_info.text = "请选择文件!"; 
   } 
   else 
   { 
    string filepath = fileupload1.postedfile.filename; 
    //if (!isallowedextension(fileupload1)) 
    //{ 
    // this.lb_info.text = "上传文件格式不正确!"; 
    //} 
    if (isallowedextension(fileupload1) == true) 
    { 
     string filename = filepath.substring(filepath.lastindexof("\\") + 1); 
     string serverpath = server.mappath("~/images/") + filename; 
     fileupload1.postedfile.saveas(serverpath); 
     this.lb_info.text = "上传成功!"; 
    } 
    else 
    { 
     this.lb_info.text = "请上传图片!"; 
    } 
   } 
  } 
  catch (exception ex) 
  { 
   this.lb_info.text = "上传发生错误!原因:" + ex.tostring(); 
  } 
 } 
 private static bool isallowedextension(fileupload upfile) 
 { 
  string stroldfilepath = ""; 
  string strextension=""; 
  string[] arrextension ={ ".gif", ".jpg", ".bmp", ".png" }; 
  if (upfile.postedfile.filename != string.empty) 
  { 
   stroldfilepath = upfile.postedfile.filename;//获得文件的完整路径名 
   strextension = stroldfilepath.substring(stroldfilepath.lastindexof("."));//获得文件的扩展名,如:.jpg 
   for (int i = 0; i < arrextension.length; i++) 
   { 
    if (strextension.equals(arrextension[i])) 
    { 
     return true; 
    } 
   } 
  } 
  return false; 
 } 
}

注意:若去掉客户端的脚本和客户端事件onclientclick="return check_filetype()",在后台代码
改为:

if (!isallowedextension(fileupload1)) 
    { 
     this.lb_info.text = "上传文件格式不正确!"; 
    } 


else if (isallowedextension(fileupload1) == true)
即变成服务器端检查上传文件类型。
5、服务器端检查上传文件的类型(文件内部真正的格式)

<body> 
 <form id="form1" runat="server"> 
 <div> 
  <table> 
   <tr> 
    <td colspan="2"> 
     服务器检查上传文件类型</td>     
   </tr> 
   <tr> 
    <td style="width: 444px"> 
     <asp:fileupload id="fileupload1" runat="server" width="432px" /></td> 
    <td style="width: 80px"> 
     <asp:button id="bt_upload" runat="server" text="上传图片" onclick="bt_upload_click" /></td> 
   </tr> 
   <tr> 
    <td colspan="2" style="height: 21px"> 
     <asp:label id="lb_info" runat="server" forecolor="red" width="515px"></asp:label></td>     
   </tr> 
  </table>  
 </div> 
 </form> 
</body>

后台代码:

using system; 
using system.data; 
using system.configuration; 
using system.web; 
using system.web.security; 
using system.web.ui; 
using system.web.ui.webcontrols; 
using system.web.ui.webcontrols.webparts; 
using system.web.ui.htmlcontrols; 
using system.io; 

public partial class _default : system.web.ui.page 
{ 
 protected void page_load(object sender, eventargs e) 
 { 

 } 
 protected void bt_upload_click(object sender, eventargs e) 
 { 
  try 
  { 
   if (fileupload1.postedfile.filename == "") 
   { 
    this.lb_info.text = "请选择文件!"; 
   } 
   else 
   { 
    string filepath = fileupload1.postedfile.filename; 
    if (isallowedextension(fileupload1) == true) 
    { 
     string filename = filepath.substring(filepath.lastindexof("\\") + 1); 
     string serverpath = server.mappath("images/") + filename; 
     fileupload1.postedfile.saveas(serverpath); 
     this.lb_info.text = "上传成功!"; 
    } 
    else 
    { 
     this.lb_info.text = "请上传图片"; 
    } 
   } 
  } 
  catch (exception error) 
  { 
   this.lb_info.text = "上传发生错误!原因:" + error.tostring(); 
  } 
 } 
 private static bool isallowedextension(fileupload upfile) 
 { 
  filestream fs = new filestream(upfile.postedfile.filename, filemode.open, fileaccess.read); 
  binaryreader r = new binaryreader(fs); 
  string fileclass = ""; 
  byte buffer; 
  try 
  { 
   buffer = r.readbyte(); 
   fileclass = buffer.tostring(); 
   buffer = r.readbyte(); 
   fileclass += buffer.tostring(); 
  } 
  catch 
  { 
    
  } 
  r.close(); 
  fs.close(); 
  if (fileclass == "255216" || fileclass == "7173"||fileclass=="6677"||fileclass=="13780")//说明255216是jpg;7173是gif;6677是bmp,13780是png;7790是exe,8297是rar 
  { 
   return true; 
  } 
  else 
  { 
   return false; 
  } 
 } 
}

为大家推荐一个专题,供大家学习:《asp.net文件上传汇总》

是不是内容很精彩,喜欢的朋友就收藏起来吧,以后在遇到asp.net文件上传问题的时候能够有所帮助。