如何获取其他服务器上的文件? 类似于这样的关系,我想通过客户客户端中获取公司服务器上的文档,并下载下来,客户中的客户端和服务端用的是wcf,而公司服务器和客户服务器只是一个数据库的连接 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以用SQL语句来获取文件:select T.c from openrowset(bulk N'D:\DB_Backup\E5KST01\audit_trail_20130419.bak', single_blob) T(c) 在你的客户端服务器的WCF服务代码里调用ReadFile方法,传入你公司服务器上文件的物理路径,即可在客户端服务器端获取到公司服务器文件的二进制流了,之后你可以保存下来,也可以直接回发给客户客户端[code=csharp]public static void ReadFile(string filePath) { string sql = "select T.c from openrowset(bulk N'" + filePath + "', single_blob) T(c)"; using (SqlConnection connection = new SqlConnection(@"Data Source=xxxxxx;Initial Catalog=master;User ID=xxxx;Password=xxx;")) { using (SqlCommand command = new SqlCommand(sql, connection)) { command.CommandType = CommandType.Text; command.CommandTimeout = 300; connection.Open(); using(var reader = command.ExecuteReader()) { if(reader.Read()) { SaveFile(reader[0] as byte[], fileName); } reader.Close(); } } } } private static void SaveFile(byte[] data, string fileName) { File.WriteAllBytes(Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, fileName), data); }[code] 获取其它服务器的文件,写一句:new WebClient().DownloadFile(......)或者一句new WebClient().DownloadData(.....) 在asp.net中的网页(或者ashx)用于下载文件,实在是有太多的帖子了,懒得贴在这里了。如果你看看网上的帖子,你会发现那些下载的文件用于检查客户端权限、用于动态输出非静态文件,用于接受客户端提交的查询(不管是get还是post方式的)参数等等。因此来说,asp.net支持服务器文件下载,是非常非常平常的事。我想纠结于wcf来用于下载文件的,一定是喜欢“时髦”而平常不搞开发的人提出来的。 为什么在方法外不能这样定义变量 C#引用DLL的问题。 在asp.net 中,C#如何显示数据库中二进制图片 科大讯飞4.0 SDK C#的开发 c#中资料类型是什么类型 i*10+i.ToString();大家进来解释一下。 生成的tiff文件发送不了传真 如何学好C# 我用datadrid和一个dataset绑定了。。我想截获delete键把他改成一个按钮,怎么作???? WPF中按钮点击回调中执行task.start无法启动 关于C#与S7-200的通讯问题 vs2008 水晶报表的问题!运行后报表不显示详细内容!
[code=csharp]
public static void ReadFile(string filePath)
{
string sql = "select T.c from openrowset(bulk N'" + filePath + "', single_blob) T(c)";
using (SqlConnection connection = new SqlConnection(@"Data Source=xxxxxx;Initial Catalog=master;User ID=xxxx;Password=xxx;"))
{
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.CommandType = CommandType.Text;
command.CommandTimeout = 300;
connection.Open();
using(var reader = command.ExecuteReader())
{
if(reader.Read())
{
SaveFile(reader[0] as byte[], fileName);
}
reader.Close();
}
}
}
} private static void SaveFile(byte[] data, string fileName)
{
File.WriteAllBytes(Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, fileName), data);
}[code]
或者一句new WebClient().DownloadData(.....)