FileUpLoad控件的使用

无人久伴 提交于 2020-02-06 10:25:20

方法一:

 

protected void btnAdd_Click(object sender, EventArgs e)
     {
         String root = System.Configuration.ConfigurationManager.AppSettings["upload"];
         bool fileOK = false;
         String save_path = root;
         String time = DateTime.Now.ToString("yyyyMMddhhmmss");
         if (!Directory.Exists(root))
         {
             Directory.CreateDirectory(root);
         }
         if (this.FileUpload1.HasFile)
         {
                         save_path += time + FileUpload1.FileName;
             String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
             String[] allowExtenstion ={ ".gif", ".png", ".bmp", ".jpg", ".jpeg", ".doc", ".txt", ".xls", ".ppt", ".rar", ".zip", ".mdb",".pdf" };
             for (int i = 0; i < allowExtenstion.Length; i++)
             {
                 if (fileExtension == allowExtenstion[i])
                 {
                     fileOK = true;
                 }
             }
             if (fileOK)
             {
                 try
                 {
                     FileUpload1.SaveAs(save_path );
                     Response.Write("<Script language='javascript'>alert('上传成功!!!');</Script>");
                 }
                 catch (Exception ex)
                 {
                     Response.Write("<Script language='javascript'>alert('上传失败'" + ex.Message + ");</Script>");
                 }
             }
         }
         else
         {
             Response.Write("<Script language='javascript'>alert('不支持的文件,请压成rar文件后再上传!');</Script>");
         }
              
           
     
}

 

方法二:

 

其实FileUpload控件使用起来很简单的
前台显示页面[fileupload.aspx]源代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="fileupload.aspx.cs" Inherits="fileupload" %>

<!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>文件上传 《FileUpload》 </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     以前在2003的时候这个控件是做在HTML里面的,要弄就得麻烦鼠标由键转成服务器控件,不知道怎么想起的,现在又做到服务器标准控件里面了,而且在属性和方法上也有很合人意的改动,不错,大家可以分享下,当然也可以看下感觉还是不错,挖哈哈!!<br /><br /><br />
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

 

到了最重要的后台代码显示了,看懂按钮中的事件代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 fileupload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string fullname = FileUpload1.FileName.ToString();//这个属性是以前2003没有的,也许是我没注意看,反正现在我才第一次用!直接取得文件名
        string url = FileUpload1.PostedFile.FileName.ToString();//这个是以前2003用的,先取得全部的上传文件路径个名字,然后再利用SubString方法来得到用户名,现在看来是没有必要了
        string typ = FileUpload1.PostedFile.ContentType.ToString();//获取文件MIME内容类型
        string typ2 = fullname.Substring(fullname.LastIndexOf(".") + 1);//获取文件名字 . 后面的字符作为文件类型
        string size = FileUpload1.PostedFile.ContentLength.ToString();

        //下面是保存了,我们来个判断,只能上穿传哪些格式的文件吧
        if (typ2 == "gif" || typ2 == "jpg" || typ2 == "bmp" || typ2 == "png")
        {
            FileUpload1.SaveAs(Server.MapPath("uploads\\image") + "\\" + fullname);//将文件保存在跟目录的uploads文件夹下
            Label1.Text = ("你上传了一个名为" + fullname + ",MIME内容类型为[" + typ + "],后缀名为[" + typ2 + "],共[" + size + "]字节大小的文件<br>已经保存在了你的UP目录中了,如果你是用VS2005,请刷新你的目录哦!!");
        }
        else
        {
            Label1.Text = "上传文件格式不正确..";
        }

    }
}


这样就完成了上传图片的操作了,上传视频也可以,但考虑到视频文件大小问题,根据经验大于5M就不是很好了,这时候需要修改Web.config配置文件了,最好还要加一个进度条,让用户清楚上传的进度...[显示进度的知识点我会尽快整理好的].

 

方法三:

string name = this.FileUpload1.FileName;
        string size = FileUpload1.PostedFile.ContentLength.ToString();
        string type = FileUpload1.PostedFile.ContentType;
        string type2 = name.Substring(name.LastIndexOf(".") + 1);
        string ipath = Server.MapPath("upimg") + "\\" + name;
        string fpath = Server.MapPath("upfile") + "\\" + name;
        string wpath = "upimg\\" + name;
        if (type2 == "jpg" || type2 == "gif" || type2 == "bmp" || type2 == "png")
        {
            FileUpload1.SaveAs(ipath);
            Image1.ImageUrl = wpath;
            Label1.Text = "Your load file is " + name + "<br>size is:" + size + "byte<br>file style is:" + type + "<br>ext name is :" + type2 + "<br>fac path is :" + ipath + "<br> vir path is:" + wpath;
        }
        else
        {
            Image1.Visible = false;
            FileUpload1.SaveAs(fpath);
            Label1.Text = "Your load file is " + name + "<br>size is:" + size + "byte<br>file style is:" + type + "<br>ext name is :" + type2 + "<br>fac path is :" + ipath + "<br> vir path is:" + wpath;
        }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!