上传文件

本文关键字:文件 | 更新日期: 2023-09-27 18:31:47

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

上传文件

若要通过 HTTP 将文件流式传输到 WCF 服务,请执行以下操作:

http://www.codeproject.com/Articles/166763/WCF-Streaming-Upload-Download-Files-Over-HTTP

但是,WebClient 类也被设计为在客户端使用。因此,您可以完全绕过 WCF 服务。

从 MSDN:

提供向 发送数据和从 接收数据的常用方法 由 URI 标识的资源。