我的代码只下载后端中存在的文件。我可以看到后端的 pdf 已正确创建并位于正确的位置,但是当我将文件发送并下载到前端并打开它时,无论我使用哪种浏览器,都会收到错误“无法加载 pdf 文档”。我认为这一定意味着我的 blob 下载代码有问题,因为我可以打开并查看后端的文件,但我无法弄清楚。我尝试过遵循网上的许多示例,但无论我尝试过什么,我都会遇到同样的问题。
server.js(节点后端文件)
app.get('/api/v1/getPdf', function(req, res) {
let resolve = require('path').resolve
res.sendFile(resolve('./tickets/tickets.pdf'));
});
Run Code Online (Sandbox Code Playgroud)
PrintDetails.js(用于下载pdf的React js代码)-注意:我只包含相关部分
class PrintDetails extends React.Component {
async printTickets() {
let file = await getTicketsPdf();
console.log(file);
const blob = new Blob([file], {type: 'application/pdf'});
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = 'tickets.pdf';
document.body.appendChild(link);
link.click();
setTimeout(function() {
document.body.removeChild(link);
}, 100);
}
render() {
return (
<div>
<button className="download-button-icon" onClick={this.printTickets}>Download</button>
</div>
)
}
async function getTicketsPdf() {
let data = {};
await (async …Run Code Online (Sandbox Code Playgroud) 我在 go 中使用 crypto 来获取密码并使用密码短语来加密密码,然后将其作为字符串存储在 postgres sql 数据库中。加密工作正常,但是当我尝试将它添加到我的数据库时,我收到一个错误,似乎表明从 []byte 到 string 类型混淆了加密密码。
func Encrypt(password string, passphrase string) string {
data := []byte(password)
block, _ := aes.NewCipher([]byte(createHash(passphrase)))
gcm, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
}
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
panic(err.Error())
}
ciphertext := gcm.Seal(nonce, nonce, data, nil)
return string(ciphertext)
}
func createHash(key string) string {
hasher := md5.New()
hasher.Write([]byte(key))
return hex.EncodeToString(hasher.Sum(nil))
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码并尝试在数据库中添加行时,出现错误
ERROR #22021 invalid byte sequence …Run Code Online (Sandbox Code Playgroud)