0

I'm trying to upload a file to the server using WebClient. My code for posting the file is as follows:

        public void UploadMultipart(byte[] file, string filename, string contentType, string url)
    {
        var webClient = new WebClient();
        string boundary = "------------------------" + DateTime.Now.Ticks.ToString("x");
        webClient.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);
        var fileData = Encoding.UTF8.GetString(file);
        var package = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n{3}\r\n--{0}--\r\n", boundary, filename, contentType, fileData);

        var nfile = webClient.Encoding.GetBytes(package);

        byte[] resp = webClient.UploadData(url, "POST", nfile);
    }

The line Encoding.UTF8.GetString(file) always returns an empty string. I've checked the byte array and it's not empty. I also tried Encoding.UTF8.GetString(file, 0, file.Length) but same result. I've read somewhere that this could be because of improper encoding. If that's the reason then how can I fix it? The file I'm trying to send is a video taken from a camera.

Any help is appreciated.

9
  • Can you post some of the contents (hex or int) of the file[] array? BTW, there seems to be some length limit in GetString - msdn.microsoft.com/en-us/library/744y86tc(v=vs.110).aspx. Don't know the behaviour if file[] is > 2048 - may as well return empty string :) Check the provided sample in the docs Commented Apr 26, 2016 at 22:09
  • the debugger is taking excruciatingly long time. dont know why :( Commented Apr 26, 2016 at 22:22
  • @Kamen okay so here's what the first byte of the array looks like postimg.org/image/jeowmhhoh Commented Apr 26, 2016 at 22:38
  • Ahmed, 0 byte indicates that it is a NUL. Strings end with a \0 which is the same and why you are getting an empty string based on your screenshot. You should verify how the data is actually being written to that array. As soon as NUL is found (encoding specific), it will stop building the string because it thinks it is done and to prevent overflows. Commented Apr 26, 2016 at 23:03
  • TyCobb What are those values in the middle? Commented Apr 26, 2016 at 23:05

1 Answer 1

1

Instead of

var fileData = Encoding.UTF8.GetString(file);

you should use

var fileData = Convert.ToBase64String(file, Base64FormattingOptions.InsertLineBreaks);

Due to the second parameter the converter will insert line breaks after every 76 characters in the string representation.

UPDATE:

You may try one of the WebClient.UploadFile() method overloads, like below:

public void UploadFile(string fileName, string url)
{
    var webClient = new WebClient();
    byte[] rawResponse = webClient.UploadFile(url, filename);
    string response = System.Text.Encoding.ASCII.GetString(rawResponse);
    Console.WriteLine(response);
}

UPDATE #2:

You may try the newer API: HttpClient.PostAsync(string, HttpContent)
The string parameter is the Uri.
The HttpContent can be an instance of MultipartFormDataContent.

You can add an instance of ByteArrayContent to the MultipartFormDataContent using its Add() method.

Maybe these will be helpful for your needs.

UPDATE #3:

Here are some possible solutions:

Sign up to request clarification or add additional context in comments.

9 Comments

data doesn't convert to a string. i see this on the log instead after that line executes :Error while resolving expression: One or more errors occurred.
maybe there's something wrong with the data file but it's not corrupted or anything. I'me using File.ReadAllBytes to get the data from the stored location.
Updated my answer with another possible solution.
That works but I'm trying to send a multipart data. Basically what I'm trying to achieve is send a byte array along with a string. any possible solutions in mind other than what I've implemented above? I preferably wanna use weClient as it's convenient to track the upload progress.
Thanks for the solution btw
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.