asp.net中上传与下载的问题
asp.net中 我用控件做的上传然后把路径存到了数据库中,该怎么做才能下载到上传的文件呢?这样做好不好? --------------------编程问答-------------------- 好不好要看用在哪里。好处是便于控制文件的锁定(相当于记录锁定),如多人打开编辑时。缺点是导致数据库过大。以下提供从数据库下载代码
using System;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
public partial class _office_down : System.Web.UI.Page
{
public string strInfo;
public string strlx;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Download(); //执行下载,可加判断是否成功
//upload();
}
}
//
private bool Download()
{
bool result = false;
string pid =Request["id"].ToString().Trim(); //取得要下载记录关键字,从前台按钮取得
string sql = @"select * from table_word where sdate='"+pid+"'"; //换成你的字段
SqlConnection conn = new SqlConnection("Data Source= (local);Persist Security Info=True;Database=office;Uid=sa;Pwd=");
//换成你的数据库链接
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
string wjlx;
//文件名取记录标题+文件类型,或你自己取名。bt是文件标题,wjlx是文件类型字段
string filename1=reader["bt"].ToString().Trim()+"."+reader["wjlx"].ToString().Trim();
//文件类型 ,以下列出三种,其它类型串可以参考网上,
switch( reader["wjlx"].ToString().Trim())
{
case "doc":
wjlx="ms-word";
break;
case "xls":
wjlx="ms-excel";
break;
case "ppt":
wjlx="ms-powerpoint";
break;
default:
wjlx="ms-word";
break;
}
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
Response.AppendHeader("Content-Disposition", "attachment;filename="+filename1); //把 attachment 改为 online 则在线打开
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
// Response.ContentType = "application/ms-excel";
Response.ContentType = "application/"+wjlx;
Page.EnableViewState = false;
Response.BinaryWrite((byte[])reader["filedata"]);
result = true;
}
reader.Close();
reader.Dispose();
cmd.Dispose();
conn.Close();
conn.Dispose();
return result;
}
//
}
--------------------编程问答-------------------- 上面filedata是存放文件的数据库字段,改成你的 --------------------编程问答-------------------- 通过一个导航控件连接到文件的物理位置,比如HyperLink --------------------编程问答-------------------- 最简单的下载
<a href="数据库中的文件路径">下载</a>
也可以用流的方式下载
在你的Page_Load中添加这样的代码:
Page.Response.Clear();
bool success = ResponseFile(Page.Request, Page.Response, "目的文件名称", @"源文件路径", 1024000);
if (!success)
Response.Write("下载文件出错!");
Page.Response.End();
public static bool ResponseFile(HttpRequest _Request,HttpResponse _Response,string _fileName,string _fullPath, long _speed)--------------------编程问答-------------------- 吧数据库里的下载文件字段取出
{
try
{
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
try
{
_Response.AddHeader("Accept-Ranges", "bytes");
_Response.Buffer = false;
long fileLength = myFile.Length;
long startBytes = 0;
double pack = 10240; //10K bytes
//int sleep = 200; //每秒5次 即5*10K bytes每秒
int sleep = (int)Math.Floor(1000 * pack / _speed) + 1;
if (_Request.Headers["Range"] != null)
{
_Response.StatusCode = 206;
string[] range = _Request.Headers["Range"].Split(new char[] {'=', '-'});
startBytes = Convert.ToInt64(range[1]);
}
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != 0)
{
//Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength));
}
_Response.AddHeader("Connection", "Keep-Alive");
_Response.ContentType = "application/octet-stream";
_Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(_fileName,System.Text.Encoding.UTF8) );
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int) Math.Floor((fileLength - startBytes) / pack) + 1;
for (int i = 0; i < maxCount; i++)
{
if (_Response.IsClientConnected)
{
_Response.BinaryWrite(br.ReadBytes(int.Parse(pack.ToString())));
Thread.Sleep(sleep);
}
else
{
i=maxCount;
}
}
}
catch
{
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch
{
return false;
}
return true;
}
就可以实现了
补充:.NET技术 , ASP.NET