日期:2014-05-18  浏览次数:20432 次

如何将图片上传到服务器上?
我想要一个能将图片上传到服务器上的一个demo 那位达人能发一个 谢了!!!

------解决方案--------------------
用html的file控件
------解决方案--------------------
HTML code
 
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
[code=C#]

------解决方案--------------------
我用的是上传 组件
------解决方案--------------------
参考下面两种情况,一是存入Database,另一个是存Folder:

http://blog.csdn.net/insus/archive/2008/01/20/2055033.aspx

http://blog.csdn.net/insus/archive/2008/01/20/2055030.aspx
------解决方案--------------------
http://www.svnhost.cn/Article/Detail-45.shtml
http://www.svnhost.cn/Article/Detail-62.shtml
这些都是例子
------解决方案--------------------
C# code
 protected void fUpload_Click(object sender, EventArgs e)
    {
        Boolean fileOk = false;
        String path = Server.MapPath("~/");
        if (FileUpload.HasFile)
        {
            String fileExtension = 
                System.IO.Path.GetExtension(FileUpload.FileName).ToLower();
            String[] allowExtension ={ ".gif", ".jpg", ".jpeg", "png" };
            for (int i = 0; i < allowExtension.Length; i++)
            {
                if (fileExtension==allowExtension[i])
                {
                    fileOk = true;
                }
            }
        }
        if (fileOk)
        {
            try
            {
                FileUpload.PostedFile.SaveAs(path + FileUpload.FileName);
                Label6.Text = "文件上传成功";
            }
            catch (System.Exception ex)
            {
                Label6.Text = "文件上传失败";
            }
        }
        else
        {
            Label6.Text = "只能上传jpg,png图片";
            Label6.BackColor = Color.Red;
        }
    }

------解决方案--------------------
C# code
  /// <summary>
    /// 上传图片
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUpLoad_Click(object sender, EventArgs e)
    {
        try
        {
            string test = Server.MapPath("ImageFiles/" + DateTime.Now.ToString("yyyy-MM-dd"));  //用来生成文件夹
            if (!Directory.Exists(test))
            {
                Directory.CreateDirectory(test);
            }
            if (FileUpload1.PostedFile.FileName != "")
            {
               string  imgname = FileUpload1.PostedFile.FileName;
               string imgType = imgname.Substring(imgname.LastIndexOf(".") + 1);
               string quanname =DateTime.Now.ToString("yyyyMMddHHmmss")+imgname.LastIndexOf("\\")+"."+imgType ;
               if ("gif" != imgType && "jpg" != imgType && "GIF" != imgType && "JPG" != imgType)
                {
                    Response.Write("<script>alert('请选择gif,jpg格式的文件!');</script>");
                    return;
                }
         
                string imgurl = "ImageFiles/" + DateTime.Now.ToString("yyyy-MM-dd") + "/" + quanname;
                 FileUpload1.PostedFile.SaveAs(Server.MapPath(imgurl));

}
}
//以下就执行更新 
                    sqlt.Operate("update dbo.NewsAdd set imgurl='" + [color=#FF0000]imgurl[/color] + "' where  newsid ='" + dtNewsID.Rows[0]["newsid"].ToString() + &q