编程论坛's Archiver

雪夜白狼 发表于 2008-4-30 21:20

求图片上传的源码!!

各位朋友谁有图片上传的代码啊
我自己写了一段可是调试老是出错?
有的话我学习一下啊!!
先谢谢了!!!

beniao 发表于 2008-5-1 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;

    }
}

雪夜白狼 发表于 2008-5-9 16:47

谢谢啊

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.