[ACCEPTED]-REST file upload with HttpRequestMessage or Stream?-wcf

Accepted answer
Score: 15

In order to test the HttpRequestMessage 5 approach I have done the following using 4 MVC:

public class TestingController : Controller
{

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Upload()
    {
        var file = Request.Files[0];
        var filename = Request.Form["filename"];
        var uri = string.Format("http://yoururl/serviceRoute/{0}", filename);
        var client = new HttpClient();
        client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("image/pjpeg"));
        var content = new StreamContent(file.InputStream);
        var response = client.PostAsync(uri, content);
        ViewBag.ServerUri = uri;
        ViewBag.StatusCode = response.Result.StatusCode.ToString();
        return View();
    }

}

The Index view should have a form in 3 it that posts back to the Upload method. Then 2 you are able to use the HttpClient to make 1 a connection to your REST service.

Score: 2

The first method is "closer to the metal" and 8 would be more flexible since you would be 7 processing the http requests and building 6 the responses yourself. If all you need 5 to do is accept a stream from a client, the 4 second option is much simpler from the implementation 3 standpoint (under the hood, it does the 2 same work that the first method is doing)

I 1 don't have an answer for your last question.

More Related questions