注册 登录
编程论坛 ASP.NET技术论坛

求图片上传的源码!!

雪夜白狼 发布于 2008-04-30 21:20, 802 次点击
各位朋友谁有图片上传的代码啊
我自己写了一段可是调试老是出错?
有的话我学习一下啊!!
先谢谢了!!!
2 回复
#2
beniao2008-05-01 16:44
回复 1# 的帖子
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 UpImage_CheckFileType : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (FileUpload1.PostedFile.FileName == "")
            {
                this.lb_info.Text = "请选择文件!";
            }
            else
            {
                string filepath = FileUpload1.PostedFile.FileName;
                if (IsPicture(FileUpload1) == true)
                {
                    string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
                    string SavaPath = Server.MapPath("UpLoad/") + filename;
                    FileUpload1.PostedFile.SaveAs(SavaPath);
                    this.lb_info.Text = "上传成功!";
                }
                else
                {
                    this.lb_info.Text = "请上传图片";
                }
            }
        }
        catch (Exception error)
        {
            this.lb_info.Text = "上传发生错误!原因:" + error.ToString();
        }


    }

    //定义实现验证上传图片的方法
    public static bool IsPicture(FileUpload hifile)
    {
        string strOldFilePath = "", strExtension = "";
        string[] arrExtension =   { ".gif", ".jpg", ".jpeg", ".bmp", ".png" };
        if (hifile.PostedFile.FileName != string.Empty)
        {
            strOldFilePath = hifile.PostedFile.FileName;
            strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
            for (int i = 0; i < arrExtension.Length; i++)
            {
                if (strExtension.Equals(arrExtension[i]))
                {
                    return true;
                }
            }
        }
        return false;

    }
}
#3
雪夜白狼2008-05-09 16:47
谢谢啊
1