Git Product home page Git Product logo

Comments (10)

lei-mu avatar lei-mu commented on June 8, 2024
  1. 代码
  2. 看请求头区别

from luch-request.

yu5056561661 avatar yu5056561661 commented on June 8, 2024

代码上都没区别,请求头一样都设置 resposetype=blod

from luch-request.

lei-mu avatar lei-mu commented on June 8, 2024

什么端?把代码发出来,你用的是 download 吗?
用uni.downloadFile 试试,所有功能都受限于uni

from luch-request.

yu5056561661 avatar yu5056561661 commented on June 8, 2024

import type {HttpResponse, HttpRequestConfig, HttpError} from 'luch-request'
import Request from 'luch-request'
// 本地环境地址

// 局域网环境地址
// const BASE_API = ''
// 线上地址
// const BASE_API = ''

const http = new Request({
//设置请求的base url
baseURL: import.meta.env.VITE_BASE_API,
//超时时长5分钟
timeout: import.meta.env.VITE_TIME_OUT,
header: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'token': ''
}
})
//请求拦截器
http.interceptors.request.use(
(config: HttpRequestConfig) => {
// 拦截添加token

	uni.showLoading({
		title: '',
		mask:true
	});
	if(config.method.toLowerCase()=="post".toLowerCase()||config.method.toLowerCase()=="put".toLowerCase()){
	    config.data.empId=getEmpId();
	}
    return config
},
(error: any) => {
    return Promise.resolve(error)
}

)

// 响应拦截器
http.interceptors.response.use(
(response: HttpResponse) => {
uni.hideLoading();
return response.data
},
(error: HttpError) => {
uni.hideLoading();
return Promise.resolve(error)
}
)

function formatData(data: any) {
switch (data.code) {
case 200:
return data.data
case 400:
uni.showToast({
title: data.msg,
icon: 'none'
})
return Promise.reject(data)
case 401:
uni.showToast({
title: data.msg,
icon: 'none'
})
return Promise.reject(data)
case 405:
// 一般是交易查询不到
return Promise.reject(data)
default:
return data
}
}

export default http

这是request ts
下面是实际使用
const config = {
method: 'get',
url: '',
responseType: 'blob',
params: { }
};
proxy.$http(config).then((res: any) => {
const blob = new Blob([res]);
const fileName = 'xx.xlsx';

	if (window.navigator.msSaveOrOpenBlob) {
		navigator.msSaveBlob(blob, fileName);
	} else {
		var link = document.createElement('a');
		link.href = window.URL.createObjectURL(blob);
		link.download = fileName;
		link.click();
	}
});

from luch-request.

yu5056561661 avatar yu5056561661 commented on June 8, 2024

uniapp H5 我发现也能返回blob 也是有内容的,但是转出来的 xlsx 就打不开, 但是 用axios 返回的blob 都能打开

from luch-request.

lei-mu avatar lei-mu commented on June 8, 2024

用download 试试。uni responseType: 'blob', 不是一个合法值

from luch-request.

yu5056561661 avatar yu5056561661 commented on June 8, 2024

我看文档上有 responseType 这个嘛

from luch-request.

lei-mu avatar lei-mu commented on June 8, 2024

image

from luch-request.

yu5056561661 avatar yu5056561661 commented on June 8, 2024

我后台是返回的流 ,这个download 也是支持流下载的吗? 怎么写还是new blob 吗?

from luch-request.

lei-mu avatar lei-mu commented on June 8, 2024
this.$http
				.get('http://localhost:3000/v1/download-image', {
					responseType: 'arraybuffer'
				})
				.then(res => {
					console.log(res);
					let blob = new Blob([res.data], {
						type: 'application/pdf;charset=UTF-8'
					});
					var a = document.createElement('a');
					document.body.appendChild(a);
					a.style = 'display: none';
					var url = window.URL.createObjectURL(blob);
					a.href = url;
					a.download = `ss.png`;
					a.click();
					a.remove();
					window.URL.revokeObjectURL(url);
				});

from luch-request.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.