谁能给个上传与下载的例子? 在上传的时候把文件直接存入oracle数据库,在下载的时候从oracle数据库中读2进制流形成文件 从而下载,谁有这方面的例子阿!很急!谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 上传文件的,没有写数据库 private Boolean SaveImages() { ///'遍历File表单元素 HttpFileCollection files = HttpContext.Current.Request.Files; /// '状态信息 System.Text.StringBuilder strMsg = new System.Text.StringBuilder(); System.Text.StringBuilder strFileName = new System.Text.StringBuilder(); strMsg.Append("上传的文件分别是:<hr color=red>"); try { for(int iFile = 0; iFile < files.Count; iFile++) { HttpPostedFile postedFile = files[iFile]; string fileName, fileExtension, UpLoadPath; fileName = System.IO.Path.GetFileName(postedFile.FileName); if (fileName != "") { fileExtension = System.IO.Path.GetExtension(fileName); //strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>"); strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br><hr>"); //strMsg.Append("上传文件的文件名:" + fileName + "<br>"); //strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>"); Common objPath=new Common(); UpLoadPath=objPath.GetCaseSubmitUplaodFileName((string)Session["CompanyID"],fileName); postedFile.SaveAs(UpLoadPath+fileName); strFileName.Append("," + UpLoadPath.Substring(UpLoadPath.LastIndexOf("\\")+1,UpLoadPath.Length-UpLoadPath.LastIndexOf("\\")-1)+fileName); } } strStatus.Text = strMsg.ToString(); strFile.Text = strFileName.ToString(); return true; } catch(System.Exception Ex) { strStatus.Text = Ex.Message; return false; } } using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;using System.IO;using System.Data.SqlClient;namespace HdFile{ /// <summary> /// 文件类。 /// </summary> public class MyFile { //定义控件 protected System.Web.UI.HtmlControls.HtmlInputFile File; private string strFileLenght="FileLenght";//文件长度 private string strFileType="FileType";//文件类型 private string strFileName="FileName";//文件名 private byte[] byteFileData;//文件数据 private byte[] intFileData=new byte[1];//文件数据 int EexIntFileLength=0; string ExeStrFileType; string ExeStrFileName; string strImgName,strImgType,strImgData; /// <summary> /// 设置文件名参数(字段名) /// </summary> public string SetFileName { set { this.strImgName=value; } } /// <summary> /// 设置文件类型参数(字段名) /// </summary> public string SetFileType { set { this.strImgType=value; } } /// <summary> /// 设置文件数据参数(字段名) /// </summary> public string SetFileData { set { this.strImgData=value; } } public MyFile() { } /// <summary> /// 上传文件(执行SQL语句) /// </summary> /// <param name="strSql">SQL语句<</param> /// <param name="conn">已经打开的SqlConnection连接对象</param> /// <param name="File">HtmlInputFile控件</param> /// <returns>返回true表示上传成功;返回false表示上传失败</returns> public bool ExeSql(string strSql,SqlConnection conn,HtmlInputFile File) { try { FileInfo file=new FileInfo(File.Value); Stream imgage=File.PostedFile.InputStream; //得到文件长度和类型 this.EexIntFileLength=File.PostedFile.ContentLength; this.ExeStrFileType = File.PostedFile.ContentType; //声名byte类型数组 byteFileData = new byte[this.EexIntFileLength]; int n = imgage.Read(byteFileData,0,this.EexIntFileLength); //得到文件名 this.ExeStrFileName=getFileName(File.Value); //更新SQL语句 string strNewSql=UpDateSql(strSql); SqlCommand cmd=new SqlCommand("",conn); cmd.CommandText=strNewSql; cmd.Parameters.Add("@FileImage",SqlDbType.Image); cmd.Parameters["@FileImage"].Value=byteFileData; cmd.ExecuteNonQuery(); return true; } catch(Exception EX) { string str_Temp=EX.Message; string str1=str_Temp; return false; } } /// <summary> /// 下载文件 /// </summary> /// <param name="conn">已经打开的SqlConnection连接对象</param> /// <param name="strSql">SQL语句</param> /// <returns>返回true表示下载成功;返回false表示下载失败</returns> public bool ExeDownloading(SqlConnection conn,string strSql) { try { SqlCommand cmd = conn.CreateCommand(); //数据库中文件ID string filename; cmd.CommandText = strSql; SqlDataReader sdr = cmd.ExecuteReader(); if(sdr.Read()) { HttpContext.Current.Response.Clear(); //写入文件名称 //正确显示中文文件名 HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312"); filename="attachment;filename="+HttpUtility.UrlEncode(sdr[this.strImgName].ToString(),System.Text.Encoding.UTF8); HttpContext.Current.Response.AppendHeader("Content-Disposition",filename); HttpContext.Current.Response.ContentType = sdr[this.strImgType].ToString(); HttpContext.Current.Response.BinaryWrite((byte[])sdr[this.strImgData]); HttpContext.Current.Response.End(); } return true; } catch { return false; } } /// <summary> /// 更新传入的SQL语句 /// </summary> /// <param name="strSql">SQL语句</param> /// <returns>更新后的SQL语句</returns> private string UpDateSql(string strSql) { string strTemp="VALUES"; string strNewSql=""; //得到VALUE后面的SQL语句 string strUpDateSql=strSql.Substring(strSql.ToUpper().LastIndexOf(strTemp)+strTemp.Length); string strNewSql2=strSql.Substring(0,strSql.ToUpper().LastIndexOf(strTemp)+strTemp.Length); try { //替换原有SQL语句的部分字段 //替换长度 strNewSql=strUpDateSql.Replace(strFileLenght.ToString(),this.EexIntFileLength.ToString()); //替换文件类型 strNewSql=strNewSql.Replace(strFileType,this.ExeStrFileType); //替换文件名 strNewSql=strNewSql.Replace(strFileName,this.ExeStrFileName); strNewSql=strNewSql2+strNewSql; return strNewSql; } catch { return ""; } } /// <summary> /// 得到文件名 /// </summary> /// <param name="pathname">HtmlInputFile控件的值</param> /// <returns>文件名</returns> private string getFileName(string pathname) { if(pathname=="") return ""; int l = 0; int j; int i = pathname.Length; while(true) { j = pathname.IndexOf("\\",l); if(j>l) l=j+1; else break; } string str = pathname.Remove(0,l); return str; } }}/////////////////////////////////////////////////////////////////调用事例private void Button2_Click(object sender, System.EventArgs e){ //============================================= //以下三个变量必须为指定的值() string strFileType="FileType";//文件类型(必须) string strFileName="FileName";//文件名(必须) string strFileData="@FileImage";//参数名称(必须) //============================================= bool boolTemp;//是否上传成功 //自定义的SQL语句 string strSql="INSERT INTO ImageStore VALUES('"+strFileName+"','"+strFileType+"',"+strFileData+",'')"; OpenDataBase();//打开数据库连接 //ExeSql()方法的三个参数: strSql INSERT 语句. conn SqlConnection连接对象(已经打开的) //File 本窗体的HtmlInputFile 控件 //返回值:返回true表示上传成功;返回false表示上传失败 boolTemp=objFile.ExeSql(strSql,conn,File); CloseDataBase();//关闭连接}private void Button1_Click(object sender, System.EventArgs e){ bool boolTemp;//是否下载成功 //自定义SQL查询语句 string strSql="select IMGTitle,IMGData,IMGType from ImageStore where id=8"; OpenDataBase();//打开数据库连接 //====================================================================== //以下三个属性的值必须是你数据表的字段名 objFile.SetFileData="IMGData";//将IMGData字段作为参数传入(必须) objFile.SetFileName="IMGTitle";//将IMGTitle字段作为参数传入(必须) objFile.SetFileType="IMGType";//将IMGType字段作为参数传入(必须) //====================================================================== //ExeDownloading()方法的确两个参数: //conn SqlConnection连接对象(已经打开的) //strSql 查询SQL 语句 boolTemp=objFile.ExeDownloading(conn,strSql); //关闭连接 CloseDataBase();} http://www.cnblogs.com/supercode/articles/173189.html 急!!!!这样的一段程序是干嘛的? 求助!平面数据表怎么生成xml树?? 帮帮我!!!第一次注册,第一次发帖,无法加载 DLL 如何读取asp中的Application 水晶报表导出pdf时出错! 請問 有对 DundasWinChart 空间有研究的同志在吗?? 进来指点我下 谢谢!! C#调用Dephi DLL 问题, 如何保存从电子表格导入的数据 在Dotnet window应用中crystal报表导出出问题,程序死掉了“未响应”,哪位碰到过这个问题啊? 求助:关于word上传至数据库的问题!(您的举手之劳,我遍散分!) .net\c#学习交流QQ群: 7246375
private Boolean SaveImages()
{
///'遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files; /// '状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
System.Text.StringBuilder strFileName = new System.Text.StringBuilder();
strMsg.Append("上传的文件分别是:<hr color=red>");
try
{
for(int iFile = 0; iFile < files.Count; iFile++)
{
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension, UpLoadPath;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName);
//strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br><hr>");
//strMsg.Append("上传文件的文件名:" + fileName + "<br>");
//strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");
Common objPath=new Common();
UpLoadPath=objPath.GetCaseSubmitUplaodFileName((string)Session["CompanyID"],fileName);
postedFile.SaveAs(UpLoadPath+fileName);
strFileName.Append("," + UpLoadPath.Substring(UpLoadPath.LastIndexOf("\\")+1,UpLoadPath.Length-UpLoadPath.LastIndexOf("\\")-1)+fileName);
}
}
strStatus.Text = strMsg.ToString();
strFile.Text = strFileName.ToString();
return true;
}
catch(System.Exception Ex)
{
strStatus.Text = Ex.Message;
return false;
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;namespace HdFile
{
/// <summary>
/// 文件类。
/// </summary>
public class MyFile
{
//定义控件
protected System.Web.UI.HtmlControls.HtmlInputFile File; private string strFileLenght="FileLenght";//文件长度
private string strFileType="FileType";//文件类型
private string strFileName="FileName";//文件名
private byte[] byteFileData;//文件数据
private byte[] intFileData=new byte[1];//文件数据
int EexIntFileLength=0;
string ExeStrFileType;
string ExeStrFileName;
string strImgName,strImgType,strImgData;
/// <summary>
/// 设置文件名参数(字段名)
/// </summary>
public string SetFileName
{
set
{
this.strImgName=value;
}
}
/// <summary>
/// 设置文件类型参数(字段名)
/// </summary>
public string SetFileType
{
set
{
this.strImgType=value;
}
}
/// <summary>
/// 设置文件数据参数(字段名)
/// </summary>
public string SetFileData
{
set
{
this.strImgData=value;
}
} public MyFile()
{ }
/// <summary>
/// 上传文件(执行SQL语句)
/// </summary>
/// <param name="strSql">SQL语句<</param>
/// <param name="conn">已经打开的SqlConnection连接对象</param>
/// <param name="File">HtmlInputFile控件</param>
/// <returns>返回true表示上传成功;返回false表示上传失败</returns>
public bool ExeSql(string strSql,SqlConnection conn,HtmlInputFile File)
{
try
{
FileInfo file=new FileInfo(File.Value);
Stream imgage=File.PostedFile.InputStream;
//得到文件长度和类型
this.EexIntFileLength=File.PostedFile.ContentLength;
this.ExeStrFileType = File.PostedFile.ContentType;
//声名byte类型数组
byteFileData = new byte[this.EexIntFileLength];
int n = imgage.Read(byteFileData,0,this.EexIntFileLength); //得到文件名
this.ExeStrFileName=getFileName(File.Value); //更新SQL语句
string strNewSql=UpDateSql(strSql);
SqlCommand cmd=new SqlCommand("",conn);
cmd.CommandText=strNewSql;
cmd.Parameters.Add("@FileImage",SqlDbType.Image);
cmd.Parameters["@FileImage"].Value=byteFileData;
cmd.ExecuteNonQuery();
return true;
}
catch(Exception EX)
{
string str_Temp=EX.Message;
string str1=str_Temp;
return false;
}
} /// <summary>
/// 下载文件
/// </summary>
/// <param name="conn">已经打开的SqlConnection连接对象</param>
/// <param name="strSql">SQL语句</param>
/// <returns>返回true表示下载成功;返回false表示下载失败</returns>
public bool ExeDownloading(SqlConnection conn,string strSql)
{
try
{
SqlCommand cmd = conn.CreateCommand();
//数据库中文件ID
string filename;
cmd.CommandText = strSql;
SqlDataReader sdr = cmd.ExecuteReader();
if(sdr.Read())
{
HttpContext.Current.Response.Clear();
//写入文件名称
//正确显示中文文件名
HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
filename="attachment;filename="+HttpUtility.UrlEncode(sdr[this.strImgName].ToString(),System.Text.Encoding.UTF8);
HttpContext.Current.Response.AppendHeader("Content-Disposition",filename);
HttpContext.Current.Response.ContentType = sdr[this.strImgType].ToString();
HttpContext.Current.Response.BinaryWrite((byte[])sdr[this.strImgData]);
HttpContext.Current.Response.End();
}
return true;
}
catch
{
return false;
}
}
/// 更新传入的SQL语句
/// </summary>
/// <param name="strSql">SQL语句</param>
/// <returns>更新后的SQL语句</returns>
private string UpDateSql(string strSql)
{
string strTemp="VALUES";
string strNewSql="";
//得到VALUE后面的SQL语句
string strUpDateSql=strSql.Substring(strSql.ToUpper().LastIndexOf(strTemp)+strTemp.Length);
string strNewSql2=strSql.Substring(0,strSql.ToUpper().LastIndexOf(strTemp)+strTemp.Length);
try
{
//替换原有SQL语句的部分字段
//替换长度
strNewSql=strUpDateSql.Replace(strFileLenght.ToString(),this.EexIntFileLength.ToString()); //替换文件类型
strNewSql=strNewSql.Replace(strFileType,this.ExeStrFileType); //替换文件名
strNewSql=strNewSql.Replace(strFileName,this.ExeStrFileName);
strNewSql=strNewSql2+strNewSql;
return strNewSql; }
catch
{
return "";
}
}
/// <summary>
/// 得到文件名
/// </summary>
/// <param name="pathname">HtmlInputFile控件的值</param>
/// <returns>文件名</returns>
private string getFileName(string pathname)
{
if(pathname=="")
return "";
int l = 0;
int j;
int i = pathname.Length;
while(true)
{
j = pathname.IndexOf("\\",l);
if(j>l)
l=j+1;
else
break;
}
string str = pathname.Remove(0,l);
return str;
} }
}///////////////////////////////////////////////////////////////
//调用事例
private void Button2_Click(object sender, System.EventArgs e)
{
//=============================================
//以下三个变量必须为指定的值()
string strFileType="FileType";//文件类型(必须)
string strFileName="FileName";//文件名(必须)
string strFileData="@FileImage";//参数名称(必须)
//=============================================
bool boolTemp;//是否上传成功
//自定义的SQL语句
string strSql="INSERT INTO ImageStore VALUES('"+strFileName+"','"+strFileType+"',"+strFileData+",'')";
OpenDataBase();//打开数据库连接 //ExeSql()方法的三个参数: strSql INSERT 语句. conn SqlConnection连接对象(已经打开的)
//File 本窗体的HtmlInputFile 控件
//返回值:返回true表示上传成功;返回false表示上传失败
boolTemp=objFile.ExeSql(strSql,conn,File);
CloseDataBase();//关闭连接
}private void Button1_Click(object sender, System.EventArgs e)
{
bool boolTemp;//是否下载成功
//自定义SQL查询语句
string strSql="select IMGTitle,IMGData,IMGType from ImageStore where id=8";
OpenDataBase();//打开数据库连接
//======================================================================
//以下三个属性的值必须是你数据表的字段名
objFile.SetFileData="IMGData";//将IMGData字段作为参数传入(必须)
objFile.SetFileName="IMGTitle";//将IMGTitle字段作为参数传入(必须)
objFile.SetFileType="IMGType";//将IMGType字段作为参数传入(必须)
//======================================================================
//ExeDownloading()方法的确两个参数:
//conn SqlConnection连接对象(已经打开的)
//strSql 查询SQL 语句
boolTemp=objFile.ExeDownloading(conn,strSql); //关闭连接
CloseDataBase();
}