【发布时间】:2021-10-18 07:23:06
【问题描述】:
如何在next.js中使用api下载txt
console.log(ninjas) 已经显示了正确的信息
我已经测试过 api postman 并且工作完美 当我在邮递员中使用与控制台登录代码相同的信息输出时 但是我如何添加功能来下载文件 所以我会弹出下载
新错误
error ReferenceError: triggerDownload is not defined
at C:\java\node\next\test\.next\server\pages\test2.js:32:147
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
我正在使用的代码
/////////////////// api scrip audiocodes
export const getStaticProps = async () => {
var myHeaders = new Headers();
myHeaders.append("Authorization", "Basic password");
var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
fetch("http://10.0.5.26/api/v1/files/ini", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.then(result => triggerDownload(result, 'board.ini'))
.catch(error => console.log('error', error));
return {
}
}
//////////////// start website
const Backup = () => {
return (
<div>
<h>Download started</h>
</div>
);
}
export default Backup;
日志console.log(ninjas)的输出文件
;**************
;** Ini File **
;**************
;Time & Date: 16/08/2021 09:13:59
;Device Up Time: 48d:21h:38m:41s
;Board: UNIVERGE BX9000
;Board Type: 72
;Serial Number: 9107130
;Software Version: 7.20A.256.721
;DSP Software Version: 5014AE3_R => 723.06
;Board IP Address: 10.0.5.26
;Board Subnet Mask: 255.255.255.0
;Board Default Gateway: 10.0.5.4
;CPU: Cavium Networks Octeon V0.1 @ 500Mhz, total 2 cores, 2 cpus, 1 sockets
;Cores mapping:
;core #0, on cpu #0, on socket #0
;core #1, on cpu #1, on socket #0
;Memory: 512 MB
;Flash size: 64 MB
;Num of DSP Cores: 3
;Num of physical LAN ports: 12
;Client defaults file is being used (file length=1573)
;;;Key features:;Board Type: 72 ;IP Media: VXML ;DATA features: FireWall&VPN ;PSTN Protocols: ISDN IUA=2 CAS ;Security: IPSEC MediaEncryption StrongEncryption EncryptControlProtocol ;Channel Type: RTP DspCh=150 ;HA ;Coders: G723 G729 GSM-FR G727 G722 ;DSP Voice features: IpmDetector ;Control Protocols: MSFT FEU=50 SIP SBC=25 ;Default features:;Coders: G711 G726;
;----- HW components -----
;
; Slot # : Module type : # of ports
;----------------------------------------------
; 1 : Empty
; 2 : Empty
; 3 : Empty
;----------------------------------------------
;USB Port 1: Empty
;USB Port 2: Empty
;----------------------------------------------
[SYSTEM Params]
SyslogServerIP =
EnableSyslog = 1
TelnetServerEnable = 2
ENABLEPARAMETERSMONITORING = 1
ActivityListToLog = 'pvc', 'afl', 'dr', 'fb', 'swu', 'naa', 'spc', 'll', 'cli', 'ae'
SyslogServerPort = 0
DayLightSavingTimeStart = '01:01:00:00'
DayLightSavingTimeEnd = '01:01:00:00'
DayLightSavingTimeEnable = 1
HALocalMAC = '00908f8ass'
TR069ACSPASSWORD = ''
TR069CONNECTIONREQUESTPASSWORD = ''
NTPServerIP = ''
SBCWizardFilename = 'templates4.zip'
PM_VEDSPUtil = '1,135,150,15'
【问题讨论】:
-
这是否回答了您的问题:how to download file in react js?
-
在那篇文章中,您建议文件已经在文件目录中的服务器上。此代码是从 api 获取文件。
-
您的文件也是如此,即
http://10.0.5.26/api/v1/files/ini?为什么需要获取它? -
下载 (GET) 操作使用应用程序/八位字节流编码。示例 GET /api/v1/files/ini HTTP/1.1 主机:10.0.5.26 HTTP/1.1 200 OK 内容类型:application/octet-stream ;************** ;** Ini File ** ;************** ;Board: Mediant SW ;Board Type: 73 ;Serial Number: 101780235059663 ;Slot Number: 1 ;Software Version: 7.20A.200.014 ;DSP Software版本:SOFTDSP => 0.00 ;板子 IP 地址:10.4.219.229 ;板子网掩码:255.255.0.0 ...
标签: javascript html node.js api next.js