我正在使用WCF web服务上传文件,下面是我的上传代码:
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[]格式发送我的文件吗?如果是这样,那么我该怎么做呢?
发布于 2013-09-18 17:28:27
要通过HTTP将文件流式传输到WCF服务,请执行以下操作:
http://www.codeproject.com/Articles/166763/WCF-Streaming-Upload-Download-Files-Over-HTTP
但是,WebClient类也被设计用于客户端。因此,您可以完全绕过WCF服务。
来自MSDN:
提供了向URI标识的资源发送数据和从资源接收数据的常用方法。
https://stackoverflow.com/questions/18863264
复制相似问题