asp.net实现文件上传,FileUpdate.aspx.cs和FileHandler.ashx代码的注释。

FileUpload.aspx.cs文件代码如下:voidbtnAdd_Click(Objects,EventArgse){if(UpFile.HasFile){if(C... FileUpload.aspx.cs文件代码如下:
void btnAdd_Click(Object s, EventArgs e){
if (UpFile.HasFile){
if (CheckFileType(UpFile.FileName)){
AddFile(UpFile.FileName, UpFile.FileContent);
rptFiles.DataBind();}
}
}
private void AddFile(string fileName, System.IO.Stream upload)
{
SqlConnection con = new SqlConnection(...)
SqlCommand cmd = new SqlCommand("INSERT File (FileName) Values (@FileName;"+"SELECT @Identity=SCOPE_IDENTITY()", con);
cmd.Parameters.AddWithValue("@FileName",fileName);
SqlParameter idParm = cmd.Parameters.Add("Identity",SqlDbType.Int);
idParm.Direction = ParameterDirection.Output;

using (con){
con.Open();
cmd.ExecuteNonQuery();
int newFileId = (int)idParm.Value;
StoreFile(newFileId,upload, con);
}
}
private void StoreFile(int fileId, Stream upload, SqlCommand connection) {
int bufferLen = 8040;
BinaryReader br = new BinaryReader(upload);
byte[] chunk = br.ReadBytes(bufferLen);

SqlCommand cmd = new SqlCommand("UPDATE File SET FileBytes=@Buffer WHERE FileId=@FileId", connection);
cmd.Parameters.AddWithValue(@FileId, fileId);
cmd.Parameters.Add("@Buffer", SqlDbType.VarBinary, bufferLen).Value = chunk;
cmd.ExecuteNonQuery();

SqlCommand cmdAppend = new SqlCommand("UPDATE File SET FileBytes .WRITE(Buffer,Null,0) WHERE FileId=@FileId", connection);
cmdAppend.Parameters.AddWithValue("@FileID", fileId);
cmdAppend.Parameters.Add("Buffer", SqlDbType.VarBinary, bufferLen);
chunk = br.ReadBytes(bufferLen);

while (chunk.Length > 0){
cmdAppend.Parameters["@Buffer"].Value = chunk;
cmdAppend.ExecuteNonQuery();
chunk = br.ReadBytes(bufferLen);
}
br.Close();
}
FileHandler.ashx代码:
展开
 我来答
互联网体验师
推荐于2016-08-23 · 超过25用户采纳过TA的回答
知道答主
回答量:102
采纳率:0%
帮助的人:74.7万
展开全部
void btnAdd_Click(Object s, EventArgs e){
if (UpFile.HasFile){//判断是否存在文件
if (CheckFileType(UpFile.FileName)){//判断文件类型
AddFile(UpFile.FileName, UpFile.FileContent);//调用AddFile方法
rptFiles.DataBind();}//数据绑定
}
}
///方法AddFile
///输入参数:文件名fileName,文件流upload
///输出参数:无
private void AddFile(string fileName, System.IO.Stream upload)
{
SqlConnection con = new SqlConnection(...)//新建数据库连接
SqlCommand cmd = new SqlCommand("INSERT File (FileName) Values (@FileName;"+"SELECT @Identity=SCOPE_IDENTITY()", con);//
cmd.Parameters.AddWithValue("@FileName",fileName);//参数赋值
SqlParameter idParm = cmd.Parameters.Add("Identity",SqlDbType.Int);
idParm.Direction = ParameterDirection.Output;

using (con){//使用using进行垃圾回收
con.Open();//打开数据库连接
cmd.ExecuteNonQuery();//执行插入语句
int newFileId = (int)idParm.Value;//将idParm的值转换成整形并赋值给newFileID
StoreFile(newFileId,upload, con);//调用保存文件方法
}
}
///保存文件方法StoreFile
///输入参数:文件编号fileID,文件流upload,连接命令connection
/// 输入参数:无
private void StoreFile(int fileId, Stream upload, SqlCommand connection) {
int bufferLen = 8040;//声明变量bufferLen并赋值8040
BinaryReader br = new BinaryReader(upload);//实例化二进制读取器,读取upload流
byte[] chunk = br.ReadBytes(bufferLen);//从流中读入bufferLen个字节数组,并使当前位置提升bufferLen,存入chunk数组中

SqlCommand cmd = new SqlCommand("UPDATE File SET FileBytes=@Buffer WHERE FileId=@FileId", connection);
cmd.Parameters.AddWithValue(@FileId, fileId);//参数赋值
cmd.Parameters.Add("@Buffer", SqlDbType.VarBinary, bufferLen).Value = chunk;//参数赋值
cmd.ExecuteNonQuery();//执行更新语句

SqlCommand cmdAppend = new SqlCommand("UPDATE File SET FileBytes .WRITE(Buffer,Null,0) WHERE FileId=@FileId", connection);
cmdAppend.Parameters.AddWithValue("@FileID", fileId);
cmdAppend.Parameters.Add("Buffer", SqlDbType.VarBinary, bufferLen);
chunk = br.ReadBytes(bufferLen);

while (chunk.Length > 0){//循环读取数据流
cmdAppend.Parameters["@Buffer"].Value = chunk;
cmdAppend.ExecuteNonQuery();
chunk = br.ReadBytes(bufferLen);
}
br.Close();//关闭BinaryReader
}
kingdomtao
2010-06-03 · 超过11用户采纳过TA的回答
知道答主
回答量:39
采纳率:0%
帮助的人:28.9万
展开全部
FileUpload.aspx.cs文件代码如下:
//点击事件
void btnAdd_Click(Object s, EventArgs e){
if (UpFile.HasFile)//上传文件是否存在{
if (CheckFileType(UpFile.FileName))//文件类型检查{
AddFile(UpFile.FileName, UpFile.FileContent);
rptFiles.DataBind();}
}
}
private void AddFile(string fileName, System.IO.Stream upload)
{
//连接数据库
SqlConnection con = new SqlConnection(...)
SqlCommand cmd = new SqlCommand("INSERT File (FileName) Values (@FileName;"+"SELECT @Identity=SCOPE_IDENTITY()", con);
cmd.Parameters.AddWithValue("@FileName",fileName);
SqlParameter idParm = cmd.Parameters.Add("Identity",SqlDbType.Int);
idParm.Direction = ParameterDirection.Output;

using (con){
con.Open();
cmd.ExecuteNonQuery();
int newFileId = (int)idParm.Value;
StoreFile(newFileId,upload, con);
}
}
//存文件
private void StoreFile(int fileId, Stream upload, SqlCommand connection) {
int bufferLen = 8040;
BinaryReader br = new BinaryReader(upload);//讲文件保存为二进制
byte[] chunk = br.ReadBytes(bufferLen);

SqlCommand cmd = new SqlCommand("UPDATE File SET FileBytes=@Buffer WHERE FileId=@FileId", connection);
cmd.Parameters.AddWithValue(@FileId, fileId);
cmd.Parameters.Add("@Buffer", SqlDbType.VarBinary, bufferLen).Value = chunk;
cmd.ExecuteNonQuery();

SqlCommand cmdAppend = new SqlCommand("UPDATE File SET FileBytes .WRITE(Buffer,Null,0) WHERE FileId=@FileId", connection);
cmdAppend.Parameters.AddWithValue("@FileID", fileId);
cmdAppend.Parameters.Add("Buffer", SqlDbType.VarBinary, bufferLen);
chunk = br.ReadBytes(bufferLen);

while (chunk.Length > 0){
cmdAppend.Parameters["@Buffer"].Value = chunk;
cmdAppend.ExecuteNonQuery();
chunk = br.ReadBytes(bufferLen);
}
br.Close();
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
depluin
2010-06-03 · TA获得超过3179个赞
知道大有可为答主
回答量:3378
采纳率:75%
帮助的人:2491万
展开全部
唔,懒得一句句写,就捡几个要点说好了..........

if (UpFile.HasFile){
if (CheckFileType(UpFile.FileName)){
AddFile(UpFile.FileName, UpFile.FileContent);
rptFiles.DataBind();}
}
如果有文件并且文件类型符合上传要求,调用AddFile上传,上传完成后更新绑定一下数据

开数据库连接什么的不说了,直接从FileUpload控件的FileContent中获取上传文件的byte流了,然后

chunk = br.ReadBytes(bufferLen);

while (chunk.Length > 0){
从FileUpload控件的FileContent循环读取数据流,
cmdAppend.Parameters["@Buffer"].Value = chunk;
传给SQL命令的参数,执行更新写入数据库

FileHandler.ashx就很简单了,根据请求field来查询数据,以word格式发送给浏览器下载。。。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式