1

This might sound crazy, but I have an MVC3 action (server-side code) that needs to post a file to another web site. I have control over both sites, but the second site never receives the posted file data. I use WebRequest to successfully request a file from the remote site, so I figured I could post the file data using this approach:

private WebRequest _SetupCopyToRequest(string source, string expectedDestination)
{
    var request = HttpWebRequest.Create(_Settings.CopyToServerUrl);
    var info = new FileInfo(source);
    request.Method = "POST";
    request.ContentLength = info.Length;
    request.ContentType = "multipart/form-data";
    var dispositionValue = String.Format("form-data; name=\"file\"; filename=\"{0}\"", expectedDestination);
    request.Headers.Set("Content-Disposition", dispositionValue);
    using (var destStream = request.GetRequestStream())
    {
        using (var sourceStream = info.OpenRead())
        {
            var length = (int) info.Length;
            var buffer = new byte[info.Length];
            sourceStream.Read(buffer, 0, length);
            destStream.Write(buffer, 0, length);
        }
    }
    return request;
}

My problem is that the action on the receiving site receives the request as soon I call request.GetRequestStream() on the first site, and Request.Files is empty on the second site. Here is the code for the receiving action on the second site:

[HttpPost]                                                           
public ActionResult CopyToServer()
{
    if (Request.Files.Count == 0 || Request.Files[0].ContentLength == 0)
        return new ContentResult();

    var file = Request.Files[0];
    var fileName = Path.GetFileName(file.FileName);
    var directory = Path.GetDirectoryName(file.FileName);
    var uniqueFileName = CeoPath.GetUniqueFileName(directory, fileName);
    var path = Path.Combine(directory, uniqueFileName);
    file.SaveAs(path);
    return new ContentResult {Content = path};
}

So how do I get my file data from the first site's server posted to the second site's server?

3 Answers 3

2

You need to use multipart/form-data encoding for uploading files. For a single file upload you could use the UploadFile method or I have blogged about a technique allowing you to upload multiple files.

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

Comments

1

My guess is that your content-type is messing things up - you're declaring that you're sending multipart form-data, but I don't see where you do proper multipart encoding:

http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.2

Comments

0

Found my answer by searching SO for HttpWebRequest and came across this one on Upload files with HttpWebRequest (multipart/form-data). Especially the link to Upload files using an RFC 1867 POST request with HttpWebRequest/WebClient. So I just replaced my first method with the following working code:

public string CopyToServer(string source, string expectedDestination)
{
    var file = new UploadFile(source, "file", null);
    var form = new NameValueCollection();
    form["expectedFileName"] = expectedDestination;
    var request = (HttpWebRequest) WebRequest.Create(_Settings.CopyToServerUrl);
    var response = HttpUploadHelper.Upload(request, new[] {file}, form);
    using (var s = response.GetResponseStream())
    {
        using (var sr = new StreamReader(s))
        {
            return sr.ReadToEnd();
        }
    }
}

Hurrah!

Comments

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.