许诺诺丶
许诺诺丶
  • 发布:2020-07-16 11:25
  • 更新:2023-08-23 20:14
  • 阅读:8015

uni.downloadFile在H5上下载网络图片时一直报跨域问题

分类:uni-app

uni.downloadFile在H5上下载网络图片时一直报跨域问题
Access to XMLHttpRequest at 'https://lylm.oss-cn-hangzhou.aliyuncs.com/apk/com.huirw.photofactory/com.huirw.photofactory_1563876108.apk' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

2020-07-16 11:25 负责人:无 分享
已邀请:
4***@qq.com

4***@qq.com

同样问题求解

DCloud_UNI_GSQ

DCloud_UNI_GSQ

需要服务端允许跨域才行,另外H5端一般情况使用不到downloadFile接口,直接使用网络地址即可。

  • wang哈哈

    哪我这就是二般情况

    2020-11-02 17:35

1***@qq.com

1***@qq.com

我也是二般情况,求解决方案

惊天

惊天 - uni 开发

研究了一天终于解决了
图片nginx下面跨域 在网站的cofig文件配置如下
location ~ ..(gif|jpg|jpeg|png|bmp|swf)$
{
add_header Access-Control-Allow-Origin
;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

    expires      30d;  
    error_log off;  
    access_log /dev/null;  
}
梦尋Junjie

梦尋Junjie - 原来她有男朋友

我是这样玩的

new Promise((resolve, reject) => {  
                // 获取网络图片路径  会牵涉跨域 那么则需要从服务器中代理  
                uni.downloadFile({  
                    url:  "/api/proxy?href="+ encodeURIComponent(this.product.appShareCover),  
                    success: res => {  
                        if (res.statusCode === 200) {  
                            resolve(res.tempFilePath);  
                        } else {  
                            reject('下载失败');  
                        }  
                    },  
                    fail: reject  
                });  
            })

后端

@RequestMapping( "proxy")  
@RestController  
public class ProxyWeb {  

    public static RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(15000)  
            .setConnectionRequestTimeout(20000).setSocketTimeout(35000).build();  

    /**  
     * 简单的代理, 不牵涉到正文内容  
     *   
     * @param request  
     * @param response  
     * @throws IOException  
     * @throws URISyntaxException  
     */  
    @RequestMapping  
    public void proxyGet(HttpServletRequest request, HttpServletResponse response)  
            throws IOException, URISyntaxException {  
        Map<String, String[]> parameterMap = request.getParameterMap();  
        String[]              href         = parameterMap.get("href");  
        if (href == null || href.length == 0 || href[0].isEmpty()) {  
            // -> 没有找到  
            response.setStatus(404);  
            response.getWriter().write("无请求地址");  
            return;  
        }  
        String                method = request.getMethod();  
        //QueryCriteria<Object> query  = new QueryCriteria<Object>();  
        parameterMap.forEach((k, v) -> {  
            if (k.equals("href")) {  
                return;  
            }  
            for (String value : v) {  
                //query.addValue(k, value);  
            }  
        });  
        HttpRequestBase http = null;  
        if (HttpGet.METHOD_NAME.equalsIgnoreCase(method)) {  
            HttpGet httpGet = new HttpGet(href[0]);  
            //query.setEntity(httpGet);  
            http = httpGet;  
        } else if (HttpPost.METHOD_NAME.equalsIgnoreCase(method)) {  
            HttpPost httpPost = new HttpPost(href[0]);  
            //query.setEntity(httpPost);  
            http = httpPost;  
        } else {  
            response.setStatus(404);  
            response.getWriter().write("只允能使用 GET|POST");  
            return;  
        }  
        http.setConfig(requestConfig);  
        // -> 发送  
        HttpClientBuilder     httpClientBuilder = HttpClientBuilder.create();  
        CloseableHttpResponse execute           = httpClientBuilder.build().execute(http);  
        HttpEntity entity = execute.getEntity();  
        response.setStatus(execute.getStatusLine().getStatusCode());  
        response.setContentType(entity.getContentType().getValue());  
        response.setContentLength((int)entity.getContentLength());  
        ServletOutputStream outputStream = response.getOutputStream();  
        InputStream content = entity.getContent();  
        content.transferTo(outputStream);  
        outputStream.flush();  
        outputStream.close();  
        content.close();  
    }  

}

QueryCriteria 类是自己写的一个请求携带参数, 如果只是简单的请求 不会用到这个

用户7810

用户7810

请求楼主解决了吗? 我downloadFile请求的是unicloud云存储的链接 也报这个错,应该怎么处理啊

要回复问题请先登录注册