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代码: 展开
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代码: 展开
展开全部
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
}
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
}
展开全部
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();
}
//点击事件
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();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
唔,懒得一句句写,就捡几个要点说好了..........
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格式发送给浏览器下载。。。
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格式发送给浏览器下载。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询