2

I have the following MVC 4 action

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SendAttachment(SomeViewModel model, HttpPostedFileBase attachment)
{
    // implementation goes here
}

Now i want to upload a file from a console app to this controller action by using HttpWebRequest API but i can't figure out how to set both model and file data in my post, so that matches the controller.

any hints on that?

esskar
  • 10,638
  • 3
  • 36
  • 57
  • Sure, This will help http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data – Khurshid Apr 17 '13 at 22:25
  • @Khurshid thanks, but it is not that i don't know how to send a file, it is more like the combination of the way i have to define the data that defines the model parameter in my post data. – esskar Apr 18 '13 at 07:01

1 Answers1

3

I made some changes that function (Upload files with HTTPWebrequest (multipart/form-data)) for your problem. It worked for me. Try like this:

Server side

    [HttpPost]
    public ActionResult SendAttachment(SomeViewModel model, HttpPostedFileBase attachment)
    {
        return View();
    }

    public class SomeViewModel  
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

In console App:

static void Main(string[] args)
    {
        UploadData(
            "http://dissertation.lan/Home/SendAttachment",
            new NameValueCollection()
            { 
                {"attachment", @"C:\Users\_____\Desktop\YourFile.xltx"}
            },
            new NameValueCollection() 
            {
                {"Id", "2"},
                {"Name","Man"}
            });
    }

    public static void UploadData(string url, NameValueCollection files, NameValueCollection nvc)
    {
        string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");

        HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
        httpWebRequest2.Method = "POST";
        httpWebRequest2.KeepAlive = true;
        httpWebRequest2.Credentials = System.Net.CredentialCache.DefaultCredentials;

        Stream memStream = new System.IO.MemoryStream();

        byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
        boundary + "\r\n");

        string formdataTemplate = "\r\n--" + boundary +
        "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";

        foreach (string key in nvc.Keys)
        {
            string formitem = string.Format(formdataTemplate, key, nvc[key]);
            byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
            memStream.Write(formitembytes, 0, formitembytes.Length);
        }

        memStream.Write(boundarybytes, 0, boundarybytes.Length);

        string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";

        foreach (string key in files.Keys)
        {
            string header = string.Format(headerTemplate, key, files[key]);
            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
            memStream.Write(headerbytes, 0, headerbytes.Length);

            FileStream fileStream = new FileStream(files[key], FileMode.Open,
            FileAccess.Read);
            byte[] buffer = new byte[1024];
            int bytesRead = 0;

            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                memStream.Write(buffer, 0, bytesRead);
            }

            memStream.Write(boundarybytes, 0, boundarybytes.Length);

            fileStream.Close();
        }

        httpWebRequest2.ContentLength = memStream.Length;

        Stream requestStream = httpWebRequest2.GetRequestStream();

        memStream.Position = 0;
        byte[] tempBuffer = new byte[memStream.Length];
        memStream.Read(tempBuffer, 0, tempBuffer.Length);
        memStream.Close();
        requestStream.Write(tempBuffer, 0, tempBuffer.Length);
        requestStream.Close();

        WebResponse webResponse2 = httpWebRequest2.GetResponse();

        Stream stream2 = webResponse2.GetResponseStream();
        StreamReader reader2 = new StreamReader(stream2);

        Console.Write(reader2.ReadToEnd());

        webResponse2.Close();
        httpWebRequest2 = null;
        webResponse2 = null;
    }

Result enter image description here

enter image description here

Community
  • 1
  • 1
Khurshid
  • 954
  • 7
  • 19