我正在使用WCF网络服务上传文件, 这是我的上传代码:
public string UploadTransactionsFile(string uploadPath)
{
string uploadTransactionsFile;
if (String.IsNullOrEmpty(uploadPath))
return string.Empty;
if (!ValidateTransactionsFile(uploadPath))
return string.Empty;
try
{
var dir = @"C:\Upload\";
string myUploadPath = dir;
var myFileName = Path.GetFileName(uploadPath);
CheckDirectory(myUploadPath);
var client = new WebClient { Credentials = CredentialCache.DefaultCredentials };
client.UploadFile(myUploadPath + myFileName, "PUT", uploadPath);
client.Dispose();
uploadTransactionsFile = "ok";
}
catch (Exception ex)
{
uploadTransactionsFile = ex.Message;
}
return uploadTransactionsFile;
}
我创建了一个Windows窗体测试客户端并添加了服务引用,但是 我调用方法的代码并硬编码了我要上传的文件:
private testServiceClient testService;
private void Button_Click(object sender, RoutedEventArgs e)
{
var File = "C:\\file.csv";
testService = new testServiceClient();
testService.UploadTransactionFile(File);
}
我可以使用一台计算机上传文件,但是当我将测试客户端放到另一台计算机上时,我不能,因为该文件只是传递了在服务器计算机上找不到的字符串路径。
我错过了什么吗?
我是否必须将文件作为byte[]
发送?如果是这样,那我该怎么做?
答案 0 :(得分:2)
通过HTTP将文件流式传输到WCF服务:
http://www.codeproject.com/Articles/166763/WCF-Streaming-Upload-Download-Files-Over-HTTP
但是,WebClient类也设计用于客户端。所以你可以完全绕过WCF服务。
来自MSDN:
提供向a发送数据和从a接收数据的常用方法 由URI标识的资源。