如何能利用VS2008开发的Web程序实现文件从本地上传到服务器和从服务器下载到本地这两功能。
在服务器端存储可以用两种办法:
1.直接将文件存到数据库中(SqlServer 2005)
2.存到服务器的某个文件夹下。
寻求最佳解决办法(最好带案例),如要用额外的控件,请提供控件下载地址。
谢谢。
在服务器端存储可以用两种办法:
1.直接将文件存到数据库中(SqlServer 2005)
2.存到服务器的某个文件夹下。
寻求最佳解决办法(最好带案例),如要用额外的控件,请提供控件下载地址。
谢谢。
if (fileUpload.HasFile)
{
string savePath = Server.MapPath("~/upload/");
if (!System.IO.Directory.Exists(savePath))
{
System.IO.Directory.CreateDirectory(savePath);
}
savePath = savePath + "\\" + fileUpload.FileName;
fileUpload.SaveAs(savePath);
}
下载
string filePath ="";
FileInfo Fi = new FileInfo(filePath);
if (Fi.Exists)
{
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=1.excel");
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
string path = Server.MapPath("~/") + "";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(obj.Name, System.Text.Encoding.GetEncoding("utf-8")));
Response.ContentType = "application/octet-stream";
Response.WriteFile("" + path + "");
Response.End();
下载时先读服务器文件,或从数据库读出数据,用Response.Write方式输出文件内容 客户端自动识别为下载例子很多
1。http://www.51aspx.com/CV/TangshanCuxiao,jxiq1v7vplr39gb7n2,photo.aspx.cs.html
2。C#.NET上传文件的保存 (回帖中)