Dakoy
Dakoy

Reputation: 53

uploading of files

I am working on uploading files with a WCF web service, here's my code for uploading:

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;
}

I created a Windows Forms test client and added the service reference, but my code in calling the method and hardcoded the file i want to upload:

private testServiceClient testService;
private void Button_Click(object sender, RoutedEventArgs e)
{
    var File = "C:\\file.csv";
    testService = new testServiceClient();

    testService.UploadTransactionFile(File);
}

I can upload files using one computer, but when I put my test client to another computer, then I can't, because the file is just passing the stringpath, which cannot be found on server computer.

Am I missing something?

Do I have to send my file as byte[]? If so, then how do I do this?

Upvotes: 0

Views: 633

Answers (1)

tom redfern
tom redfern

Reputation: 31800

To stream files over HTTP to WCF service:

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

However, WebClient class is designed to be used on the client side too. So you could bypass the WCF service altogether.

From MSDN:

Provides common methods for sending data to and receiving data from a resource identified by a URI.

Upvotes: 2

Related Questions