Asp.net的FileUpload控件的文件上传与Extjs文件上传的简单Demo

Asp.net的FileUpload控件的文件上传与Extjs文件上传的简单Demo
点击:44次  来源:   时间:2011-08-16 20:42

 

当我在说“愿上帝保佑女人”这句话时,尽管我们之中没有人能完全懂得一位贤妻的高贵情怀,或是一位良母执着奉献,但贰心中会说“阿门”!一、可能思路

精力象乳汁一样是可以养育的,聪明便是一只乳房。1.1、Asp.net的FileUpload控件的文件上传

1.2、Extjs的文件上传

二、数据库(暂无)

三、新建一个网站

3.1、Default.aspx代码

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






   

    

    

    
    
    
    


    

3.2、Default.aspx.cs代码

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        /** 文件是否为空 **/
        bool fileIsValid = false;

        if (FileUpload1.HasFile)
        {
            /** 获取上传文件的后缀 **/
            string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
            string[] restrictExtension = { ".gif", ".jpg", ".bmp", ".png" };
            /** 断定文件类型是否合适请求 **/
            for (int i = 0; i < restrictExtension.Length; i++)
            {
                if (fileExtension == restrictExtension[i])
                {
                    fileIsValid = true;
                }
            }
        }
        /** 若是文件类型合适请求,调用SaveAs办法实现上传,并显示相干信息 **/
        if (fileIsValid == true)
        {
                try
                {
                    Image1.ImageUrl = "~/" + FileUpload1.FileName;
                    FileUpload1.SaveAs(Server.MapPath("~/") + FileUpload1.FileName);
                    Label1.Text="文件上传成功";
                    Label1.Text+="
"; Label1.Text+="
  • "+"原文件路径:"+FileUpload1.PostedFile.FileName; Label1.Text+="
    "; Label1.Text+="
  • "+"文件大小:"+FileUpload1.PostedFile.ContentLength+"字节"; Label1.Text+="
    "; Label1.Text+="
  • "+"文件类型:"+FileUpload1.PostedFile.ContentType; } catch { Label1.Text="文件上传成功"; } } else { Label1.Text="只可以或许上传后缀为.gif,.jpg,.bmp,.png的文件夹"; } } }
  • 3.3、upload.ashx代码

    <%@ WebHandler Language="C#" Class="upload" %>
    
    using System;
    using System.Web;
    
    using System.IO;
    public class upload : IHttpHandler
    {
    
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            UpLoadImage(context);
        }
        string serverpath = "";
        string savepath = "";
        private string json = "";
        public void UpLoadImage(HttpContext content)
        {
            try
            {
                if (content.Request.Files.Count > 0)
                {
                    HttpPostedFile postedFile = content.Request.Files[0];
                    serverpath = content.Server.MapPath("~/");
                    savepath = serverpath + Path.GetFileName(postedFile.FileName);
                    if (File.Exists(savepath))
                    {
                        json = "{success:false,""msg"":""该图片已经存在!""}";
                    }
                    else
                    {
                        postedFile.SaveAs(savepath);
                        json = "{success:true,""msg"":""上传成功!""}";
                    }
                }
                else
                {
                    json = "{success:false,""msg"":""获取文件异常!""}";
                }
            }
            catch (Exception ex)
            {
                json = "{success:false,""msg"":""" + ex.Message + "!""}";
            }
            content.Response.Write(json);
            content.Response.End();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    
    }

    四、结果演示

    五、总结

    上传方面斗劲写得斗劲简单些(还可以扩大一些文件上的存储),像一些博客揭晓文章、数据库存储(图片名称、接见路径)还有编辑本身头像等等,断定稍微有些错杂,不过花些时候应当也不会太难。

    六、源代码供给

    6.1、运行景象

    体系:Win7

    IDE对象:VS2008

    版本:.net framework3.5

    Extjs版本:Extjs 3.2.0

    6.2、源代码

    Asp.net上传文件Demo:http://files.cnblogs.com/yongfeng/Asp.net%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0Demo.rar

    你可能感兴趣的:(extjs,asp.net,stylesheet,upload,function,button)