使用ASP.NET上传图片汇总(三)

来源:互联网 发布:mysql查询结果中文乱码 编辑:程序博客网 时间:2024/06/05 00:46
 

6 产生不重复文件名及自动生成路径

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head id="Head1" runat="server">
  5. <title>上传图片</title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <table>
  11. <tr>
  12. <td colspan="2">
  13. </td>
  14. </tr>
  15. <tr>
  16. <td style="width: 444px">
  17. <asp:FileUpload ID="FileUpload1" runat="server" Width="432px" /></td>
  18. <td style="width: 80px">
  19. <asp:Button ID="bt_upload" runat="server" Text="上传" OnClick="bt_upload_Click" Width="72px" /></td>
  20. </tr>
  21. <tr>
  22. <td colspan="2" style="height: 21px">
  23. <asp:Label ID="lb_info" runat="server" ForeColor="Red" Width="515px"></asp:Label></td>
  24. </tr>
  25. </table>
  26. </div>
  27. </form>
  28. </body>
  29. </html>

后台代码:

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. public partial class _Default : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. }
  15. protected void bt_upload_Click(object sender, EventArgs e)
  16. {
  17. upLoad();
  18. }
  19. protected void upLoad()
  20. {
  21. //定义了用于保存Session会话中的数据,定义了获取文件名和文件的扩展名
  22. //string ShowSellerName;
  23. string FileName, FileLastName;//文件名,文件的扩展名
  24. try
  25. {
  26. FileName = FileUpload1.FileName;//获取文件名
  27. FileLastName = FileName.Substring(FileName.LastIndexOf(".") + 1);//得到文件的扩展名
  28. //ShowSellerName = System.Web.HttpContext.Current.Session["SellerName"].ToString().Trim();
  29. Random R = new Random();//创建产生随机数
  30. int val = 10 + R.Next(99);//产生随机数为99以内任意
  31. string sc = val.ToString();//产生随机数
  32. string FileTime = DateTime.Now.ToString("yyyyMMddHHmmss") + sc;//得到系统时间(格式化)并加上随机数以便生成上传图片名称
  33. string UploadFileName = FileTime +"." + FileLastName;//产生上传图片的名称
  34. string UD = "ShowSellerName";//创建用户的文件夹的名字
  35. string path = System.Web.HttpContext.Current.Server.MapPath("ProductsImages").ToString() +"//" + UD + "//";
  36. if (!System.IO.Directory.Exists(path))//如果文件夹不存在则创建.确定给定路径是否引用磁盘上的现有目录, 如果 path 引用现有目录,则为 true;否则为 false。
  37. {
  38. try
  39. {
  40. System.IO.Directory.CreateDirectory(path);//创建文件夹与用户名同名
  41. if (FileUpload1.HasFile)
  42. {
  43. FileUpload1.PostedFile.SaveAs(path + UploadFileName);//上传图片(名称自定义)
  44. string User_ProductsInageUrl ="ProductsImages/" + UD + "/" + UploadFileName;//保存获得的图片虚拟路径进行跨页面间的传递
  45. }
  46. else
  47. {
  48. System.Web.HttpContext.Current.Response.Redirect("~/Tips/Error.aspx");//显示出错信息
  49. }
  50. }
  51. catch
  52. {
  53. System.Web.HttpContext.Current.Response.Redirect("~/Tips/Error.aspx");//显示出错信息
  54. }
  55. }
  56. else
  57. {
  58. if (FileUpload1.HasFile)//判断FileUpload组件是否存在内容
  59. {
  60. FileUpload1.PostedFile.SaveAs(path + UploadFileName);//上传图片(自定义)
  61. string User_ProductsImageUrl ="ProductsImages/" + UD + "/" + UploadFileName;//得到服务端图片的虚拟路径
  62. System.Web.HttpContext.Current.Session["ImageUrl"] = User_ProductsImageUrl;//保存获得的图片虚拟路径进行跨页面间的传递
  63. }
  64. else
  65. {
  66. System.Web.HttpContext.Current.Response.Redirect("~/Tips/Error.aspx");//显示出错信息
  67. }
  68. }
  69. }
  70. catch
  71. {
  72. System.Web.HttpContext.Current.Response.Redirect("~/Tips/Error.aspx");//显示出错信息
  73. }
  74. }
  75. }

原创粉丝点击