IE与Google中文件上传、导出文件区别

一、文件上传

IE中file会把文件所在物理路径带上 后台用

HttpFileCollection files = HttpContext.Current.Request.Files;

 foreach (string key in files.AllKeys){

HttpPostedFile file = files[key];

var fileName = file.FileName;

string pattern = "[\\[ \\] \\^ \\-_*×――(^)$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,;\"‘’“”-]";
fileName = Regex.Replace(fileName, pattern, "_");//过滤特殊字符
var filePath = (uploadPath + fileName).Replace(@"/", @"\");
string tmpRootDir = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath.ToString());
string fileSavePath = tmpRootDir + filePath;
//文件存在即删除
if (File.Exists(fileSavePath))
{
File.Delete(fileSavePath);
}
file.SaveAs(fileSavePath);

}

此时Google浏览器没有问题 但是IE会提示找不到“x:xx文件夹下文件” 。

所以要把var fileName = file.FileName;改成Path.GetFileName(file.FileName);//获取文件名

二、文件导出乱码

Workbook wb = new Workbook();

var fileName = "空房房间_" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls";
string filePath = sPath + "\\" + fileName;
wb.Save(filePath);
var stream = FileToStream(filePath);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.ms-excel");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = fileName;

这样Google不会乱码 但是IE会 所以要加上 fileName = HttpUtility.UrlEncode(fileName);//对字符串进行编码

posted @ 2019-10-15 09:27  艺洁  阅读(455)  评论(0编辑  收藏  举报