/// <summary>
/// Down load file
/// </summary>
/// <param name="path">file path</param>
/// <returns></returns>
public virtual Stream DownLoadFile(string path) {
Stream ms = new MemoryStream();
if (!File.Exists(path))
return null;
using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read)) {
//将文件转为二进制,
long fileSize = fileStream.Length;
byte[] bytes = new byte[fileSize];
fileStream.Read(bytes, 0, bytes.Length);
fileStream.Close();
string name = path.Substring(path.LastIndexOf("\\") + 1);
WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";
WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-Disposition", string.Format("attachment;filename=\"{0}\";filename*=utf-8'' {1}", name, name));
return new MemoryStream(bytes);
}
}
Web调用方法:
const url = this.appService.rootUrl + '/DownLoadFile?path='+documentPath;
location.href = url;