Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

javascript - Ajax在Heroku上无法获取文件夹中的图像文件(Ajax to get image files in folder not working on Heroku)

Using this Ajax code, I pull image files and randomly add them to a div of several pages works locally on my system.

(使用此Ajax代码,我提取图像文件并将其随机添加到在我的系统上本地工作的几个页面的div中。)

    var folder = "Views/assets/images";
    debugger;
    $.ajax({
        url: folder,
        success: function (data) {
            var patt1 = /"([^"]*.(jpe?g|png|gif))"/gi;     // extract "*.jpeg" or "*.jpg" or "*.png" or "*.gif"
            var result = data.match(patt1);
            result = result.map(function (el) {
                return el.replace(/"/g, "");
            });     // remove double quotes (") surrounding filename+extension // TODO: do this at regex!

            var uniqueNames = [];                               // this array will help to remove duplicate images
            $.each(result, function (i, el) {
                var el_url_encoded = encodeURIComponent(el);    // avoid images with same name but converted to URL encoded
                // console.log("under analysis: " + el);
                if ($.inArray(el, uniqueNames) === -1 && $.inArray(el_url_encoded, uniqueNames) === -1) {
                    // console.log("adding " + el_url_encoded);
                    uniqueNames.push(el_url_encoded);
                    $("#slider").append("<img src='" + el_url_encoded + "' alt=''>");      // finaly add to HTML
                }
                // else{   console.log(el_url_encoded + " already in!"); }
            });
            $(".grid li").each((i, li) => {
                let bg = folder + uniqueNames[Math.floor(Math.random() * uniqueNames.length) + 1];
                $(li).css('background-image', 'url(' + bg + ')');
                setInterval(() => {
                    let bg = folder + uniqueNames[Math.floor(Math.random() * uniqueNames.length) + 1];
                    $(li).css('background-image', 'url(' + bg + ')');
                }, 20000);
            })
            console.log(uniqueNames)
        },
        error: function (xhr, textStatus, err) {
            alert('Error: here we go...');
            alert(textStatus);
            alert(err);
            alert("readyState: " + xhr.readyState + "
 xhrStatus: " + xhr.status);
            alert("responseText: " + xhr.responseText);
        }
    });

i noticed the browser url link appears as file:///C:/Users/, but it started to fumble once i migrated it to a localhost:5000 and consequently when i push it to the Heroku dyno.

(我注意到浏览器的URL链接显示为file:/// C:/ Users /,但是一旦我将其迁移到localhost:5000并因此将其推送到Heroku dyno时,它就会开始崩溃。)

I am thinking there is a security of some sort that wouldnt let me into the sub-directories/directories.

(我认为存在某种安全机制,不会让我进入子目录/目录。)

Thanks in advance

(提前致谢)

  ask by TREX_SON translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...