感谢您使用微软产品。在上传的时候,我们可以使用<input type="file" runat=server...> HTML ServerControl, 然后在服务器端得到对应的文件流,把文件流写入数组,然后倒入数据库。下面是我写的一个示例程序:File1为HTML File ServerControl对象:注意:假设数据库为test_00, 表为imageFile, 内有两个子段,fileName和fileData.////////////////////////////////////////////////////////////////////////////
if( ( File1.PostedFile != null ) && ( File1.PostedFile.ContentLength > 0 ) )
{
string SourceFilePath=this.File1.PostedFile.FileName;
string DBcon=@"server=localhost;integrated security=yes;database=test_00";
System.IO.Stream fs=this.File1.PostedFile.InputStream; bool b=Handler.FileToSqlBlob(SourceFilePath,DBcon,fs);
Response.Write(b.ToString());
}else
{
Response.Write("Please select a file to upload.");
}下面这个函数,接受三个参数,文件名,数据库连接串,流对象, 然后把流读入数组,计入数据库。////////////////////////////////////////////////////////////////////////////public static bool FileToSqlBlob(string SourceFilePath,string ConString,System.IO.Stream fs)
{
string fileName="";
bool bresult=true;
try
{
fileName=System.IO.Path.GetFileName(SourceFilePath);
SqlConnection cn=new SqlConnection(ConString);
SqlCommand cmd=new SqlCommand(@"Insert into imagefile(filename,filedata) values( @FileName,@FileData)",cn);
Byte[] b=new Byte[fs.Length];
fs.Read(b,0,b.Length);
fs.Close(); SqlParameter pFileName=new SqlParameter("@FileName",SqlDbType.NVarChar,fileName.Length,ParameterDirection.Input,false,0,0,null,DataRowVersion.Current,fileName);

SqlParameter pFileData=new SqlParameter("@FileData",SqlDbType.VarBinary, b.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, b);

cmd.Parameters.Add(pFileName);
cmd.Parameters.Add(pFileData);

cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}

catch(SqlException ex)
{
bresult=false;
}
return bresult;
}////////////////////////////////////////////////////////////////////////////同时,我们可以通过下面的代码,把数据库中的文件读出,通过Response.OutputStream,发送到浏览器端:////////////////////////////////////////////////////////////////////////////
string DBcon=@"server=localhost;integrated security=yes;database=test_00";

// Response.ContentType="Application/x-msexcel";Response.ContentType="Application/msword";this.Response.Clear();
Handler.SqlBlobToFile(Session["file"].ToString(),DBcon,this.Response.OutputStream);
this.Response.End();
下面是SqlBlobToFile函数的实现:
////////////////////////////////////////////////////////////////////////////
public static bool SqlBlobToFile(string fileName,string ConString, System.IO.Stream fs)
{

bool bresult=true; try
{
int fileDataCol = 0; 
SqlConnection cn=new SqlConnection(ConString);
SqlCommand cmd=new SqlCommand(@"SELECT fileData FROM imagefile WHERE filename='"+fileName +@"'",cn);

cn.Open(); SqlDataReader dr=cmd.ExecuteReader();
dr.Read();

Byte[] b = new Byte[(dr.GetBytes(fileDataCol, 0, null, 0, int.MaxValue))];
dr.GetBytes(fileDataCol, 0, b, 0, b.Length);
dr.Close();

cn.Close(); fs.Write(b,0,b.Length);
fs.Close();
} catch(SqlException ex)
{
bresult=false;
} return bresult;}////////////////////////////////////////////////////////////////////////////希望对您有所帮助。
 
-微软全球技术中心 
 
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。