请原谅我,因为代码很乱。我还在学习。
我需要从CSV文件下载带有URL扫描的图像。然而,我有2000+的URL与相同的域,我不认为服务器将让我拉动一切,因此我总是得到一些图像后的错误。我需要解决的问题 -
1)如何确保图像完全下载然后只有代码移动到下一个URL
2)如何编写更好的代码
你的帮助表示赞赏。谢谢
var csv = require('fast-csv');
var Promise = require('bluebird');
var fs = require('fs');
var request = require('request');
var path = "test.csv";
var promiseCSV = Promise.method(function(path, options) {
return new Promise(function(resolve, reject) {
var records = [];
csv
.fromPath(path, options)
.on('data', function(record) {
records.push(record);
})
.on('end', function() {
resolve(records);
console.log('done');
});
});
});
var download = function(uri, filename, callback){
request.head(uri, function(err, res, body){
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
};
promiseCSV(path).then(function (records) {
for(i=0;i<records.length;i++)
{
download(records[i][0],'img/'+records[i][1], function(){
});
}
});
答案 0 :(得分:1)
这会一次限制您的请求。另一种选择是使用throttled-request来限制每单位时间的请求。
Private Sub btnImport_Click()
Dim bookList As Workbook
Dim mergeObj As Object, dirObj As Object, filesObj As Object, everyObj As Object
Dim folderpath As String
folderpath = Range("I5").Value
If Range("I5").Value = "" Then
MsgBox "Select the folder which contains the reports.", vbInformation, "Cannot Import."
Range("I2").Select
ElseIf FileFolderExists(folderpath) = False Then
MsgBox "Selected Folder Does Not Exist.", vbInformation, "Cannot Import."
Range("I5").Select
ElseIf Dir(folderpath, vbDirectory) = "" Then
MsgBox "Selected Folder Not Found.", vbInformation, "Invalid Folder Name."
Range("I5").Select
Else
Me.lblWait.Visible = True
Me.btnCancel.Visible = False
Me.btnImport.Visible = False
Application.ScreenUpdating = False
Application.StatusBar = "Collecting Data, Please Wait..."
Set mergeObj = CreateObject("Scripting.FileSystemObject")
'change folder path of excel files here
Set dirObj = mergeObj.Getfolder("folderpath")
Set filesObj = dirObj.Files
For Each everyObj In filesObj
Set bookList = Workbooks.Open(everyObj)
On Error Resume Next
'Change B3:H to the range your working on and also B in B65536 to any column required.
bookList.Worksheets(1).Range("B3:H350" & bookList.Worksheets(1).Range("B65536").End(xlUp).Row).Copy
ThisWorkbook.Worksheets(1).Activate
'Below only change "B" column name to your required column name
Range("B65536").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Application.CutCopyMode = False
bookList.Close
Next
End If
End Sub
此外,您的记录变量超出范围,您需要将其移出才能访问它:
var i = 0;
promiseCSV(path).then(function (records) {
next();
function next(){
download(records[i][0],'img/'+records[i][1], function(){
i++;
if (i < records.length) next();
});
}
});