0

I have a WebClient and I am trying to upload a file programmatically in C#.

While uploading the file using the real webform I get the following problem:

------WebKitFormBoundary8huFCK1rAfUxxul6
Content-Disposition: form-data; name="__EVENTTARGET"


------WebKitFormBoundary8huFCK1rAfUxxul6
Content-Disposition: form-data; name="__EVENTARGUMENT"


------WebKitFormBoundary8huFCK1rAfUxxul6
Content-Disposition: form-data; name="__VIEWSTATE"

The code I am using to upload the file is as follows:

internal void UploadFile(System.IO.FileInfo cvFileInfo)
    {
        if (cvFileInfo.Exists == false)
            throw new FileNotFoundException("The Specified file was not found.");

//I know this part is wrong >
        var values = new NameValueCollection
        {
            { "Content-Disposition: form-data; name=\"__EVENTTARGET\""        , ""                  } ,
            { "Content-Disposition: form-data; name=\"__EVENTARGUMENT\""      , ""                  } ,
            { "Content-Disposition: form-data; name=\"__VIEWSTATE\""          , _viewstate          } ,
            { "Content-Disposition: form-data; name=\"__VIEWSTATE\""          , _viewstategenerator } ,
            { "Content-Disposition: form-data; name=\"__EVENTVALIDATION\""    , _eventvalidation    } ,
            { "Content-Disposition: form-data; name=\"ctl00$main$CVFile\"; filename=\"Kiran Randhawa - Curriculum vitae - Copy.docx\" Content-Type: application/octet-stream", Encoding.UTF8.GetString(File.ReadAllBytes(cvFileInfo.FullName)) },
            { "Content-Disposition: form-data; name=\"ctl00$main$btnUpload\"" , "Upload Now"         } ,
        };

        var returnedBytes = client.UploadValues(_uploadCVUrl, values);
        var returnedString = System.Text.Encoding.UTF8.GetString(returnedBytes);

        if (returnedString != null)
        {

        }
    }

How do I fix this in such a way that it produces the desired packet output.

Many Thanks, Kiran

Sarath Subramanian
  • 20,027
  • 11
  • 82
  • 86
K-Dawg
  • 3,013
  • 2
  • 34
  • 52
  • http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data – CodeCaster Nov 11 '14 at 11:00
  • Code caster, Thanks for this but I think the problem is that I want to use a cookie container with a web client. rather then a raw web request. I do understand that this might not be possible to achieve with a web client and I might need lower level access. I'd obviously prefer to use the web-client if possible. – K-Dawg Nov 11 '14 at 11:12
  • See the [HttpClient answer](http://stackoverflow.com/a/16925159/266143). – CodeCaster Nov 11 '14 at 11:14
  • code caster I think it has to be a webclient. The reason being is that I have inherited the web client so that I can add some additional functionality to it (adding support for cookies for example). – K-Dawg Nov 11 '14 at 11:22
  • Then see [Since WebClient's uploadData doesn't encode data, then what will be the effect of adding a “Content-Type”, “multipart/form-data” header to it](http://stackoverflow.com/questions/12785411/since-webclients-uploaddata-doesnt-encode-data-then-what-will-be-the-effect-o), where it is explained that in `OpenWriteCompleted` you can modify the output. – CodeCaster Nov 11 '14 at 11:24
  • Code caster... Thank you! That's perfect! – K-Dawg Nov 11 '14 at 11:25

0 Answers0