本文介绍了如何使用C#windows窗体将文件上传到Web服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要帮助,假设我有Windows窗体包含打开的对话框和按钮,以便
uploade file to webserver (http:// localhost :79 / uploade /)
我怎么做这个
please
推荐答案
private void uploadButton_Click(object sender, EventArgs e)
{
var openFileDialog = new OpenFileDialog();
var dialogResult = openFileDialog.ShowDialog();
if (dialogResult != DialogResult.OK) return;
Upload(openFileDialog.FileName);
}
private void Upload(string fileName)
{
var client = new WebClient();
var uri = new Uri("http://www.yoursite.com/Uploader/");
{
client.Headers.Add("fileName", System.IO.Path.GetFileName(fileName));
client.UploadFileAsync(uri, fileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
服务器:
in server:
[HttpPost]
public async Task<object> Uploader()
{
var file = await Request.Content.ReadAsByteArrayAsync();
var fileName =Request.Headers.GetValues("fileName").FirstOrDefault();
var filePath = "/upload/files/";
try
{
File.WriteAllBytes(HttpContext.Current.Server.MapPath(filePath) + fileName, file);
}
catch (Exception ex)
{
// ignored
}
return null;
}
这篇关于如何使用C#windows窗体将文件上传到Web服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!