已知服务器上文件路径:server.mapth(“文件名”);
如何用c#实现将这个文件下载到客户端的指定文件夹内,比如:c:\temp
不用ftp的下载方法,有没有其他的方法啊?
请不要把网上ftp下载文件的代码发给我。谢谢!
如何用c#实现将这个文件下载到客户端的指定文件夹内,比如:c:\temp
不用ftp的下载方法,有没有其他的方法啊?
请不要把网上ftp下载文件的代码发给我。谢谢!
<a href="http://localhost:8080/test /excelModel20087343.rar"></a>
<a href="http://localhost:8080/test /excelModel20087343.rar"> </a>就是罗,,,
同意
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;namespace Server
{
class Program
{
static void Main(string[] args)
{
//创建Socket, 并监听
EndPoint point = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6688);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Bind(point);
server.Listen(100);
Console.WriteLine("server is running...");
while (true)
{
//接收到请求, 创建线程来处理该请求
Socket socket = server.Accept();
Console.WriteLine(DateTime.Now.ToString() + ": " + socket.RemoteEndPoint.ToString() + "connected");
Thread thread = new Thread(new ParameterizedThreadStart(DownloadFile));
thread.Start(socket);
}
} /// <summary>
/// 下载文件
/// </summary>
/// <param name="obj"></param>
static void DownloadFile(object obj)
{
Socket socket = obj as Socket;
List<byte> list = new List<byte>(); int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int dataCount = 0; while ((dataCount = socket.Receive(buffer)) > 0)
{
for (int i = 0; i < dataCount; i++)
{
list.Add(buffer[i]);
}
}
socket.Shutdown(SocketShutdown.Receive); //读文件数据到内存
string filePath = Encoding.Default.GetString(list.ToArray());
byte[] bytes = null;
if (System.IO.File.Exists(filePath))
{
FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
stream.Flush();
stream.Close();
} //发送
socket.Send(bytes);
socket.Shutdown(SocketShutdown.Send);
}
}
}
客户端:using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;namespace Client
{
class Program
{
static void Main(string[] args)
{
//服务器地址: 127.0.0.1
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 6688);
Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp); client.Connect(ipEndPoint); //发送数据
client.Send(System.Text.Encoding.Default.GetBytes(@"D:\test.txt"));
client.Shutdown(SocketShutdown.Send); //接收数据
List<byte> list = new List<byte>();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int dataCount = 0; while ((dataCount = client.Receive(buffer)) > 0)
{
for (int i = 0; i < dataCount; i++)
{
list.Add(buffer[i]);
}
}
client.Shutdown(SocketShutdown.Receive); //将数据写入文件
MemoryStream ms = new MemoryStream(list.ToArray());
FileStream stream = new FileStream(@"d:\t.txt", FileMode.Create);
ms.WriteTo(stream);
ms.Flush();
stream.Flush();
stream.Close();
ms.Close(); Console.WriteLine("OK");
Console.ReadKey();
}
}
}
string FullFileName = Page.Server.MapPath("Uploadfiles/") + strFileName;\\文件路径 FileInfo DownloadFile = new FileInfo(FullFileName);
Response.Clear();
Response.Charset = "utf-8";
Response.Buffer = false;
this.EnableViewState = false;
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition","attachment;filename=" + strFileName);
Response.ContentType = "application/unknown ";
Response.WriteFile(FullFileName);
Response.Flush();
Response.Close();
Response.End();
http://blog.csdn.net/y_qingbin/archive/2008/11/01/3197103.aspx
http://blog.csdn.net/y_qingbin/archive/2008/11/01/3197103.aspx
这样就可以下载吧