.net从远程下载文件的问题
我的项目在本地上传文件和下载文件都好用;之后把项目放在服务器上后;上传没有问题,下载的时候,我是指定放在 E:/files/文件下 ; 却下载载到服务器的E:/files/上了;怎么下载到本地自己的电脑上?数据库 A表 字段 filePath 是存放文件的路径的;
string filePath ="E:\test\ShopWeb\Admin\Upload\file\1.jpg"; //从数据库读取文件的路径
WebClient wlent = new WebClient();
if (!Directory.Exists("E:/files/"))
{
DirectoryInfo dif = Directory.CreateDirectory("E:/files/");
}
string d = filePath.Substring(filePath.LastIndexOf("\\"));
wlent.DownloadFile(filePath, "E:/files/" + d);
请高手指点!!!。。谢谢 --------------------编程问答-------------------- /// <summary>
/// 打开服务器上文件下载至本地
/// </summary>
/// <param name="fullFileName">服务器上文件全路径</param>
/// <param name="realFileName">下载保存文件名</param>
/// <param name="delFile">是否删除文件</param>
public static void TryToDisplayFile(string fullFileName, string realFileName, bool delFile)
{
System.IO.FileInfo fileInfo = new System.IO.FileInfo(fullFileName);
if (fileInfo.Exists == true)
{
const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
byte[] buffer = new byte[ChunkSize];
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
System.IO.FileStream iStream = System.IO.File.OpenRead(fullFileName);
long dataLengthToRead = iStream.Length;//获取下载的文件总大小
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(realFileName, Encoding.UTF8).Replace("+", "%20"));
HttpContext.Current.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
while (dataLengthToRead > 0 && HttpContext.Current.Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
HttpContext.Current.Response.OutputStream.Write(buffer, 0, lengthRead);
HttpContext.Current.Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
HttpContext.Current.Response.Close();
iStream.Close();
if (delFile)
{
fileInfo.Delete();
}
HttpContext.Current.Response.End();
}
} --------------------编程问答-------------------- 两种方法:
1,使用write或流下载
2、使用超链接下载 --------------------编程问答-------------------- 使用相对路径
using (System.Net.WebClient client = new System.Net.WebClient())
{
client.DownloadFile("", "");
}
WebClient wc = new WebClient();
Byte[] bytes = wc.DownloadData("");
using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate))
{
fs.Write(bytes, 0, bytes.Length);
}
ftpwebrequest
补充:.NET技术 , ASP.NET