我尝试将文件上传到C#Winform中的本地REST服务器。点击按钮时,几次都没有发生任何事情。但是在点击几下按钮后,文件就会上传。我想知道为什么它有时会起作用,为什么它有时不起作用。我之前在VS 2010上开发了它,然后我搬到了VS 2017.
private void btnUpload_Click(object sender, EventArgs e)
{
try
{
var client = new RestClient(baseUrl + "/upload_file");
var request = new RestRequest();
request.Method = Method.POST;
request.RequestFormat = DataFormat.Json;
request.AddHeader("Authorization", "Bearer " + loginUser.Token);
request.AddHeader("Content-Type", "multipart/form-data");
request.AddFile("userphoto",
filePath, MimeTypeHelper.GetMimeType(filePath));
request.AddParameter("username", loginUser.Username);
IRestResponse response = client.Execute(request);
if (response.StatusCode == HttpStatusCode.Created)
{
Console.WriteLine(response.Content);
MessageBox.Show("The file uploaded!");
}
}
catch (WebException ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("Not updated photo!");
}
}