WPF Upload
WPF 图片上传(同步)
private void btnStart_Click(object sender, RoutedEventArgs e)
{
UpLoadFile("http://127.0.0.1:8090/api/upload.file", "D:/file/defaultfrg.png");
}
private void UpLoadFile(string uriString, string filePath)
{
//文件名
string fileName = System.IO.Path.GetFileName(filePath);
//文件格式
string fileExt = filePath.Substring(filePath.LastIndexOf(".") + 1);
WebClient client = new WebClient();
client.Credentials = CredentialCache.DefaultCredentials;
client.Headers.Add("Content-Type", "application/form-data");
client.QueryString["fname"] = fileName;
byte[] fileb = client.UploadFile(new Uri(uriString), "POST", filePath);
string res = Encoding.UTF8.GetString(fileb);
MessageBox.Show(res);
}
WPF图片上传(异步)
private void btnStart_Click(object sender, RoutedEventArgs e)
{
UpLoadFileAsyn("http://127.0.0.1:8090/api/upload.file", "D:/file/defaultfrg.png");
}
private void UpLoadFileAsync(string uriString, string filePath)
{
//文件名
string fileName = System.IO.Path.GetFileName(filePath);
//文件格式
string fileExt = filePath.Substring(filePath.LastIndexOf(".") + 1);
WebClient client = new WebClient();
//定义事件,上传成功事件和上传进度事件
client.UploadFileCompleted += client_UploadFileCompleted;
client.UploadProgressChanged += client_UploadProgressChanged;
client.Credentials = CredentialCache.DefaultCredentials;
client.Headers.Add("Content-Type", "application/form-data");//注意头部必须是form-data
client.QueryString["fname"] = fileName;
//异步上传,通过事件回调获取运行状态
client.UploadFileAsync(new Uri(uriString), "POST", filePath);
}
//上传进度事件,修改进度条
void client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
progressBar.Minimum = 0;
progressBar.Maximum = (int)e.TotalBytesToSend;
progressBar.Value = (int)e.BytesSent;
}
//上传完成事件,判断是否上传成功
void client_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
{
if (e.Error == null)
{
progressBar.Value = progressBar.Maximum;
Thread.Sleep(500);
progressBar.Visibility = Visibility.Collapsed;
}
else
{
MessageBox.Show("上传错误:" + e.Error.Message);
}
}
注意:Header的Content-Type必须设置为 application/form-data
WebApi接收图片
[RoutePrefix("api")]
public class UploadController : ApiController
{
/// <summary>
/// Content-Type:application/octet-stream
/// </summary>
[HttpPut,Route("upload.binary")]
public string UploadBinary(HttpPostedFileBase file)
{
//保存文件到根目录 App_Data + 获取文件名称和格式
var filePath = HttpContext.Current.Server.MapPath("~/App_Data/") + HttpContext.Current.Request.Form["fileName"];
using (FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
//读取文件流
BinaryReader br = new BinaryReader(HttpContext.Current.Request.Files[0].InputStream);
//将文件留转成字节数组
byte[] bytes = br.ReadBytes((int)HttpContext.Current.Request.Files[0].InputStream.Length);
//将字节数组追加到文件
bw.Write(bytes);
}
}
return "1";
}
/// <summary>
/// Content-Type:application/form-data
/// </summary>
[HttpPost, Route("upload.file")]
public string UploadFile()
{
var request = HttpContext.Current.Request;
if (request.Files.Count > 0)
{
HttpPostedFile file = request.Files.Get(0);
file.SaveAs(System.IO.Path.Combine("D:", file.FileName));
}
return "1";
}
}
WebApi Config 设置大文件上传
<configuration>
<system.web>
<httpRuntime targetFramework="4.5" maxRequestLength="2147483647" />
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483647"/>
</requestFiltering>
</security>
</system.webServer>
</configuration>
https://my.oschina.net/u/4321531/blog/3485112

浙公网安备 33010602011771号