gpt4 book ai didi

javascript - 如何下载、重命名和保存用户输入生成的图像

转载 作者:行者123 更新时间:2023-12-01 01:08:29 24 4
gpt4 key购买 nike

我正在构建一个网络应用程序,用户将输入旧文件名新文件名,然后他将能够下载生成的文件。

我是编码新手,特别是 Javascript,所以我决定请你们看看,也许可以帮助我找到解决方案。

enter image description here

我尝试使用 Jszip,但不知道如何配置它来获取用户的输入而不是预定义的 URL。

<html>
<head>
</head>
<body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip-utils/0.0.2/jszip-utils.min.js"></script>
<script>
var urls = [
"https://cors-anywhere.herokuapp.com/https://img.tenniswarehouse-europe.com/new_big/AMSPP-BL-1.jpg",
"https://cors-anywhere.herokuapp.com/https://img.tenniswarehouse-europe.com/new_big/AMSPP-BL-2.jpg",
"https://cors-anywhere.herokuapp.com/https://img.tenniswarehouse-europe.com/new_big/AMSPP-BL-3.jpg",
"https://cors-anywhere.herokuapp.com/https://img.tenniswarehouse-europe.com/new_big/AMSPP-BL-4.jpg",
"https://cors-anywhere.herokuapp.com/https://img.tenniswarehouse-europe.com/new_big/AMSPP-BL-5.jpg",
"https://cors-anywhere.herokuapp.com/https://img.tenniswarehouse-europe.com/new_big/AMSPP-BL-6.jpg",
"https://cors-anywhere.herokuapp.com/https://img.tenniswarehouse-europe.com/new_big/AMSPP-BL-7.jpg"];
var nombre = $scope.meetingName;
//The function is called
compressed_img(urls,nombre);

function compressed_img(urls,nombre) {
var zip = new JSZip();
var count = 0;
var name = nombre+".zip";
urls.forEach(function(url){
JSZipUtils.getBinaryContent(url, function (err, data) {
if(err) {
throw err;
}
var filename = url.replace(/.*\//g, "");
zip.file(filename, data, { binary: true, createFolders: true });
count++;
if (count == urls.length) {
zip.generateAsync({type:'blob'}).then(function(content) {
saveAs(content, name);
});
}
});
});
}
</script>
<br/>
<br/>
<center>
Click the button to generate a ZIP file
<br/>
<input id="button" type="button" onclick="compressed_img(urls,nombre)" value="Create Zip"></input>
</center>
</body>
</html>

我希望下载并重命名用户指定的文件,而不是作为示例的预定义 URL。

最佳答案

function compressed_img(urls,nombre) {
var zip = new JSZip();
var count = 0;
var name = `${nombre}.zip`;
var givenName = `test`;
var counter = 1;
urls.forEach(function(url){
JSZipUtils.getBinaryContent(url, function (err, data) {

console.log(data)
if(err) {
throw err;
}
var extention = url.split('.').pop();
zip.file(`${givenName}${counter}.${extention}`, data, { binary: true, createFolders: true });
count++;
counter++;
if (count == urls.length) {
zip.generateAsync({type:'blob'}).then(function(content) {
saveAs(content, name);
});
}
});
});
}

关于javascript - 如何下载、重命名和保存用户输入生成的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55383161/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com