问题
I uploaded a file by webclient. But upload success and response link file. But when I go to the file manager I don't have the file in my account. Why ?
This is my code.
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;
}
回答1:
In windows side:
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;
}
In server side you should use 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;
}
来源:https://stackoverflow.com/questions/23127250/c-sharp-upload-file-by-webclient