我正在使用 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[]
? 如果是这样,那我该怎么做?