赞
踩
先贴代码
- tapDown(fileId) {
- const userInfo = this.$apiCache.getUserInfo();
- uni.downloadFile({
- url: 'http://192.168.100.4:31173/My_OAModule/ResourceFile/downFile?loginMark=' + userInfo.loginMark + '&token=' + userInfo.token + '&data=' + fileId,
- success: function(res) {
- //安卓端 没有问题
- //苹果端 因为下载文件 后台会返回中文下载文件名 IOS 需要 url解码
- const filePath = res.tempFilePath; //临时文件路径
- //const decodePath = decodeURIComponent(filePath); //解码后路径
- //plus.runtime.openFile(filePath);
- uni.openDocument({
- filePath: filePath,
- success: function(res) {
- console.log('打开文档成功');
- }
- });
- },
- fail: function(err) {
- //console.log(err);
- }
- });
- }
前台代码就是这样,主要解决方案 放在后台 设置请求头
- /// <summary>
- /// 普通下载
- /// </summary>
- /// <param name="FileName">文件虚拟路径</param>
- /// /// <param name="name">返回客户端名称</param>
- public static void DownLoadold(string FileName, string name)
- {
- string destFileName = FileName;
- if (File.Exists(destFileName))
- {
- FileInfo fi = new FileInfo(destFileName);
- HttpContext.Current.Response.Clear();
- HttpContext.Current.Response.ClearHeaders();
- HttpContext.Current.Response.Buffer = false;
-
- //这段代码在IOS 浏览器下载时候 会出现 文件名乱码
- //HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));
-
- //主要代码这里设置头信息
- StringBuilder headerValue = new StringBuilder();
- headerValue.Append("attachment;");
- headerValue.Append(" filename=\"" + encodeURIComponent(name) + "\";");
- headerValue.Append(" filename*=utf-8''" + encodeURIComponent(name));
-
- HttpContext.Current.Response.AppendHeader("Content-Disposition", headerValue.ToString());
- HttpContext.Current.Response.AppendHeader("Content-Length", fi.Length.ToString());
- HttpContext.Current.Response.ContentType = "application/octet-stream";
- HttpContext.Current.Response.WriteFile(destFileName);
- HttpContext.Current.Response.Flush();
- HttpContext.Current.Response.End();
- }
- }
-
-
- /// <summary>
- /// 符合 RFC 3986 标准的“百分号URL编码”在这个方法里,空格会被编码成%20,而不是+和浏览器的encodeURIComponent行为一致
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- private static string encodeURIComponent(string value)
- {
- try
- {
- return HttpUtility.UrlEncode(value, Encoding.UTF8).Replace("\\+", "%20");
- }
- catch (Exception ex)
- {
- HttpContext.Current.Response.Write("Error:" + ex.Message);
- return null;
- }
- }
搞了一下午 总结下
大致原因就是 苹果设备自带浏览器 与 windows上面的浏览器的解析编码不一致,你也可以在苹果端下载个360 或者其他浏览器试试
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。