文件上传模块

页面设计:

HTML:

 1 <body>
 2     <form id="form1" runat="server">
 3     <div>
 4     
 5         <table cellpadding="0" cellspacing="0" style="width: 100%">
 6             <tr>
 7                 <td style="text-align: center">
 8                     上传图片</td>
 9             </tr>
10         </table>
11         <asp:Panel ID="Panel1" runat="server">
12             <table cellpadding="0" cellspacing="0" style="width: 100%">
13                 <tr>
14                     <td colspan="2" style="text-align: center">
15                         支持文件类型:jpg,gif</td>
16                 </tr>
17                 <tr>
18                     <td style="width: 187px; text-align: right">
19                         选择文件</td>
20                     <td>
21                         <input ID="File1" type="file" runat="server" /></td>
22                 </tr>
23                 <tr>
24                     <td colspan="2">
25                         &nbsp;</td>
26                 </tr>
27                 <tr>
28                     <td colspan="2" style="text-align: center">
29                         <asp:Button ID="btnUpload" runat="server" Text="上传" onclick="btnUpload_Click" />
30                     </td>
31                 </tr>
32             </table>
33         </asp:Panel>
34         <asp:Panel ID="Panel2" runat="server">
35         <table cellpadding="0" cellspacing="0" style="width: 100%">
36             <tr>
37                 <td colspan="2" style="text-align: center">
38                     文件上传成功</td>
39             </tr>
40             <tr>
41                 <td style="text-align: center; width: 279px">
42                     文件地址:</td>
43                 <td>
44                     <input id="txtCopyUrl" type="text" runat="server" /></td>
45             </tr>
46             <tr>
47                 <td colspan="2" style="text-align: center">
48                     <input id="Button1" type="button" value="复制到黏贴板" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
49                     <input id="Button2" type="button" value="预览图像" onclick="window.open('../' + txtCopyUrl.value,'Preview')" /></td>
50             </tr>
51         </table>
52         </asp:Panel>
53         <br />
54     
55     </div>
56     </form>
57 </body>
58 </html>
59 <script type="text/javascript">
60 function DoCopy()
61 {
62     if (document.form1.txtCopyUrl.value != "")
63     {
64         document.form1.txtCopyUrl.select();
65         textRange = document.form1.txtCopyUrl.createTextRange();
66         textRange.execCommand("Copy");
67     }
68 }
69 </script>

CS

 1 public partial class Admin_Admin_UpFile : System.Web.UI.Page
 2 {
 3     protected void Page_Load(object sender, EventArgs e)
 4     {
 5 
 6     }
 7     protected void btnUpload_Click(object sender, EventArgs e)
 8     {
 9         string filePateh = "Upload/" + OperateControl.Upload(this.File1, Server.MapPath("../UpLoad"));
10         this.Panel1.Visible = false;
11         this.Panel2.Visible = true;
12         this.txtCopyUrl.Value = filePateh;
13     }
14 }

OperateControl:

 1 public static string Upload(HtmlInputFile file, string savefile)
 2     {
 3         string fileName = file.PostedFile.FileName;
 4         string extendName = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();
 5         string newName = null;
 6         if (extendName == "jpg" || extendName == "bmp")
 7         {
 8             DateTime now = DateTime.Now;
 9             newName = now.Year.ToString() + now.Month.ToString() + now.Day.ToString() + now.Hour.ToString() + now.Minute.ToString() + now.Second.ToString();
10             file.PostedFile.SaveAs(savefile + "/" + newName + "." + extendName);
11         }
12         return newName + "." + extendName;
13     }
posted @ 2012-06-08 22:50  阿杜008  阅读(624)  评论(1)    收藏  举报