R
R
Roman Rakzin2016-10-26 20:31:39
ASP.NET
Roman Rakzin, 2016-10-26 20:31:39

How to send file via http from C# program?

You need to send a file from a C# application to a web server and then receive the data. I look at examples, but it does not work. How to organize the sending of the file?
I do so

string uri = "http://mysite/api/postfile";
            string localPath = @"c:\path_to_file.doc";
            var parameters = new System.Collections.Specialized.NameValueCollection()
            {
                { "parametr1", "parametr1_Value" }
            };

            using (var client = new WebClient())
            {
                client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                client.QueryString = parameters;
                var responseBytes = client.UploadFile(uri, localPath);
                var response = Encoding.UTF8.GetString(responseBytes);
                Console.WriteLine("\n Ответ \n {0}", response);
            }

On the server accepts the controller
public class PostFileController : Controller
    {
        [HttpPost]
        public ActionResult Index(HttpPostedFileBase upload)
        {
            //Вот здесь я хочу принять файл и записать его
            return Content("ура- файл записан"); 

        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Петр, 2016-10-26
@petermzg

А где в примерах просят заголовки менять?
Тут не меняют.
Для больших бинарных данных всегда используется multipart/form-data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question