C#通过网络客户端上传文件
本文关键字:文件 客户端 网络 | 更新日期: 2023-09-27 17:58:40
我通过web客户端上传了一个文件。但是上传成功和响应链接文件。但当我去文件管理器时,我的账户中没有文件。为什么?
这是我的密码。
private void btnUpload_Click(object sender, EventArgs e)
{
WebClient wc = new WebClient();
wc.Headers.Add("OurSecurityHeader", "encryptedvalue");
wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0");
wc.Headers.Add(HttpRequestHeader.Cookie, "__cfduid=d56b9e4ca0801822e9231936c70518ec91397746478931; __utma=259844498.1111893290.1397796877.1397796877.1397802609.2; __utmc=259844498; __utmz=259844498.1397796877.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); login=KimJanParkC1; xfss=g7prlsjg15zl57h4; __zlcid=%7B%22mID%22%3A%22OPdgp3o75YUWIg%22%2C%22sid%22%3A%22140417.91047.473AFH5T%22%7D; __utmb=259844498.17.10.1397802609; sthumb=500x500; _mcnc=1");
wc.UploadProgressChanged += new UploadProgressChangedEventHandler(wc_UploadProgressChanged);
wc.UploadFileCompleted += new UploadFileCompletedEventHandler(UploadFileCallback);
wc.UploadFileAsync(new Uri("http://img102.imagetwist.com/cgi-bin/upload.cgi?upload_id="), "POST", txtPath.Text);
}
void wc_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
{
// GET DOWNLOAD LINK
MessageBox.Show("Upload Finished");
}
void wc_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
pgbStatus.Maximum = (int)e.TotalBytesToSend;
pgbStatus.Value = (int)e.BytesSent;
label6.Text = ((int)e.BytesSent * 100) / (int)e.TotalBytesToSend + "%";
}
public void UploadFileCallback(Object sender, UploadFileCompletedEventArgs e)
{
// GET RESPOND DOWNLOAD LINK
HtmlAgilityPack.HtmlDocument hd = new HtmlAgilityPack.HtmlDocument();
hd.LoadHtml(System.Text.Encoding.UTF8.GetString(e.Result));
txtResult.Text = hd.DocumentNode.InnerHtml;
}
在窗口侧:
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();
client.UploadFileCompleted += new UploadFileCompletedEventHandler(Completed);
client.UploadProgressChanged += new UploadProgressChangedEventHandler(ProgressChanged);
var uri = new Uri("http://www.yoursite.com/UploadFile/");
try
{
client.Headers.Add("fileName", System.IO.Path.GetFileName(fileName));
var data = System.IO.File.ReadAllBytes(fileName);
client.UploadDataAsync(uri, data);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Completed(object sender, UploadFileCompletedEventArgs e)
{
MessageBox.Show(e.Error?.Message ?? "Uploaded Successfully!");
}
private void ProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
在服务器端,您应该使用WebApi:
[HttpPost]
public async Task<object> UploadFile()
{
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;
}