最近我在做一个图书在线销售系统,可是想把图片上传到数据库中,在网上找了些代码,但运进时出现了以下问题了,哪位高手帮看一下,需要怎么更改:表的部份字段如下:
[BookID] [int] ,
 [ImageData] [image] NULL ,                             
    [ImageContentType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    [ImageDescription] [varchar] (200) COLLATE Chinese_PRC_CI_AS NULL ,
    [ImageSize] [int] NULL UpLoadImage.aspx文件完全代码如下:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UpLoadImage.aspx.cs" Inherits="UpLoadImage" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>上传图片</title>
</head><body bgcolor="#FFFFFF">
<form enctype="multipart/form-data" RUNAT="server" ID="Form1">
<TABLE RUNAT="server" WIDTH="700" ALIGN="left" ID="Table1" cellpadding="0" cellspacing="0" border="0">
<TR>
     <TD>上传图片(选择你要上传的图片)</TD>
<TD>
<INPUT TYPE="file" ID="UP_FILE" RUNAT="server" STYLE="Width:320" 
ACCEPT="text/*" NAME="UP_FILE">
</TD>
</TR>
<TR>
     <TD> 
      文件说明(添加上传图片说明,如:作者、出处)
     </TD>
<TD>
<asp:TextBox RUNAT="server" WIDTH="239" ID="txtDescription" MAINTAINSTATE="false" />
</TD>
</TR>
<TR>
<TD>
<asp:Label RUNAT="server" ID="txtMessage" FORECOLOR="red" MAINTAINSTATE="false" />
</TD>
<TD>
<asp:Button ID="Button1" RUNAT="server" WIDTH="239" onCLICK="Button_Submit" TEXT="Upload Image" />
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>UpLoadImage.aspx.cs文件的完全代码如下:using System;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls; 
namespace UploadImage

public class UploadImage : Page { 
protected HtmlInputFile UP_FILE;          //HtmlControl、WebControls控件对象
protected TextBox txtDescription;
protected Label txtMessage;
protected Int32 FileLength = 0;          //记录文件长度变量 
protected void Button_Submit(System.Object sender, System.EventArgs e) {
HttpPostedFile UpFile = UP_FILE.PostedFile;  //HttpPostedFile对象,用于读取图象文件属性
FileLength = UpFile.ContentLength;     //记录文件长度 
try {
if (FileLength == 0) {   //文件长度为零时
txtMessage.Text = "<b>请你选择你要上传的文件</b>"; 
} else {
Byte[] FileByteArray = new Byte[FileLength];   //图象文件临时储存Byte数组
Stream StreamObject = UpFile.InputStream;      //建立数据流对像
//读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
StreamObject.Read(FileByteArray,0,FileLength);   
//建立SQL Server链接
SqlConnection Con = new SqlConnection("Data Source=Localhost;Initial Catalog=testdb;User ID=sa;Pwd=;");
String SqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType, ImageDescription, ImageSize) valueS (@Image, @ContentType, @ImageDescription, @ImageSize)";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@Image",SqlDbType.Binary, FileLength).value = FileByteArray;
CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar,50).value =UpFile.ContentType;  //记录文件类型
//把其它单表数据记录上传
CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar,200).value = txtDescription.Text;
//记录文件长度,读取时使用
CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt,8).value = UpFile.ContentLength;
Con.Open();
CmdObj.ExecuteNonQuery(); 
Con.Close();
txtMessage.Text = "<p><b>OK!你已经成功上传你的图片</b>";//提示上传成功
}
} catch (Exception ex) {
txtMessage.Text = ex.Message.ToString();
}
}
}
}运行时,出现以下的这样错误:
1:“ASP.uploadimage_aspx.GetTypeHashCode()”: 没有找到适合的方法来重写 c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\趣味书屋\10e69f28\270b9033\App_Web_yyde3dad.0.cs 869
2:“ASP.uploadimage_aspx.ProcessRequest(System.Web.HttpContext)”: 没有找到适合的方法来重写 c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\趣味书屋\10e69f28\270b9033\App_Web_yyde3dad.0.cs 874
3:“ASP.uploadimage_aspx”不实现接口成员“System.Web.IHttpHandler.IsReusable” c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\趣味书屋\10e69f28\270b9033\App_Web_yyde3dad.0.cs 164这是怎么回事呢?各位帮帮我啊!!先谢过了!!

解决方案 »

  1.   

     /// <summary>
        /// 上传图片并显示出来/并保存到隐藏域路径.以待点击预览图片查看图片
        protected void iUpLoad_Click(object sender, ImageClickEventArgs e)
        {
            string test = Server.MapPath("Products");  //用来生成文件夹
            if (!Directory.Exists(test))
            {
                Directory.CreateDirectory(test);
            }
            int filesize = 4096;
            if (fUpLoad.PostedFile.FileName != "")
            {
                if (fUpLoad.PostedFile.ContentLength / 4096 > filesize)
                {
                    Page.RegisterStartupScript("Startup", "<script>alert('单张产品展示图片不能超过4096K(4M),请重新选择产品展示图片上传。');</script>");
                }
                else
                {
                        string imgname = fUpLoad.PostedFile.FileName;
                        string imgType = imgname.Substring(imgname.LastIndexOf(".") + 1);
                        string quanname = Guid.NewGuid() + "." + imgType;
                        string imgurl = "Products/" + quanname;
                        fUpLoad.PostedFile.SaveAs(Server.MapPath(imgurl));
                        this.HFurl.Value = "Products" + "/" + quanname;
                        Page.RegisterStartupScript("starup", "<script>alert('产品展示图片上传成功。点击预览查看产品展示图片。');</script>");
                    
                }
            }    }
     this.HFurl.Value把这个存到数据库就行了。这里我是把图片上传地址放到一个隐藏域里面了
      

  2.   

     //上传用户图片
            try
            {
                if (fileBrowser.PostedFile.FileName != "")
                {
                    //获取图片的物理路径
                    string filepath = fileBrowser.PostedFile.FileName;
                    //获取图片的后缀名
                    string filename = filepath.Substring(filepath.LastIndexOf(".") + 1);
                    //获取保存图片的实际路径
                    string serverpath = Server.MapPath("~/images/Users/") + txtUserId.Value + "." + filename;
                    fileBrowser.PostedFile.SaveAs(serverpath);
                }
            }
            catch
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('图片上传失败!');", true);
                return;
            }   
      

  3.   

     if (File1.PostedFile.FileName != "")
            {
                   string strPath = File1.PostedFile.FileName;
                    string extension = Path.GetExtension(File1.PostedFile.FileName).ToUpper();
                    string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
                    strPath = strPath.Substring(strPath.LastIndexOf("\\") + 1);
                    File1.PostedFile.SaveAs(strPhotoPath +fileName + extension);
               
            }
    保存路径到数据库