FileUpload--example

在VS2008中,使用<asp:UploadFile控件

前台页面

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileUpload.aspx.cs" Inherits="WebApplication1.FileUpload" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml" >  
  6. <head runat="server">  
  7.     <title>无标题</title>  
  8.     <script type="text/javascript">  
  9.         function checkType() {  
  10.             //得到上传文件的值  
  11.             var fileName = document.getElementById('FileUpLoad1').value;  
  12.             //返回String对象中字符串最后出现的位置。  
  13.             var seat = fileName.lastIndexOf(".");  
  14.             //返回位于String对象中指定位置的子字符串并转换为小写  
  15.             var extension = fileName.substring(seat).toLowerCase();  
  16.             var allowed = [".xls", ".xlsx"];  
  17.             for (var i = 0; i < allowed.length; i++) {  
  18.                 if (!(allowed[i] != extension)) {  
  19.                     return true;  
  20.                 }  
  21.             }  
  22.             alert("不支持" + extension + "格式");  
  23.             return false;  
  24.         }  
  25.     </script>  
  26. </head>  
  27. <body>  
  28.     <form id="form1" runat="server">  
  29.     <div>  
  30.         <asp:FileUpload ID="FileUpLoad1" runat="server" />  
  31.         <br />  
  32.         <asp:Button ID="btnFileUpload" runat="server"   
  33.             Text="文件上传" OnClientClick="return checkType()"   
  34.             onclick="btnFileUpload_Click" />  
  35.         <asp:Label ID="lblMessage" runat="server"></asp:Label>  
  36.     </div>  
  37.     </form>  
  38. </body>  
  39. </html>  


 

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;

namespace WebApplication1
{
    public partial class FileUpload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnFileUpload_Click(object sender, EventArgs e)
        {
            if (FileUpLoad1.HasFile)
            {
                //判断文件是否小于4MB
                if (FileUpLoad1.PostedFile.ContentLength < 4194304)
                {

                   HttpPostedFile file = FileUpLoad1.PostedFile;
                   string str=file.FileName;
                   string filename2 = str.Substring(str.LastIndexOf(".")).ToString().Trim();
                   string filename1 = DateTime.Now.Ticks.ToString()+filename2;
                   file.SaveAs(Server.MapPath("./upload/"+filename1));//将用户上传的文件保存到服务器上.                    

 string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("./upload/"+filename1) + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
                    OleDbConnection Oleconn = new OleDbConnection(strConn);
                    string strExcel = "";
                    OleDbDataAdapter excelCommand = null;
                    DataSet excel_ds = new DataSet();
                    strExcel = "select * from [sheet1$]";
                    try
                    {
                        Oleconn.Open();
                        excelCommand = new OleDbDataAdapter(strExcel, Oleconn);
                        excelCommand.Fill(excel_ds, "exdtSource");//得到dataset
                        lblMessage.Text = "上传成功!";
                    }
                    catch (Exception ex)
                    {
                        lblMessage.Text = "出现异常,无法上传!";
                    }
                    finally
                    {
                        Oleconn.Close();
                        Oleconn.Dispose();
                    }

                }
                else
                {
                    lblMessage.Text = "上传文件不能大于10MB!";
                }
            }
            else
            {
                lblMessage.Text = "尚未选择文件!";
            }
            
        }
    }
}

posted on 2012-03-08 14:18 青蛙學堂 阅读(356) 评论(0)  编辑 收藏 引用 所属分类: 軟件布袋數據庫

只有注册用户登录后才能发表评论。
<2012年3月>
26272829123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

常用链接

留言簿(8)

随笔分类

随笔档案

收藏夹

青蛙学堂

最新评论

阅读排行榜

评论排行榜