Answer the question
In order to leave comments, you need to log in
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);
}
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question