0

Now that I have finished saving the image to the local, but the browser is saved to the default folder, how do I modify the code to save to the specified folder?

function download(canvas,type) {                
    var imgdata = canvas.toDataURL(type);

    var fixtype = function (type) {
        type = type.toLocaleLowerCase().replace(/jpg/i, 'jpeg');
        var r = type.match(/png|jpeg|bmp|gif/)[0];
        return 'image/' + r;
    };

    imgdata = imgdata.replace(fixtype(type), 'image/octet-stream')

    var saveFile = function (data, filename) {
        var link = document.createElement('a');

        link.href = data;
        link.download = filename;

        var event = document.createEvent('MouseEvents');
        event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
        link.dispatchEvent(event);
    };

    saveImageCount++;

    var str = "" +saveImageCount;
    var pad = "0000";
    var ans = pad.substring(0, pad.length - str.length) + str;
    var filename = ans + '.' + type;

    saveFile(imgdata, filename);
}
Liam
  • 27,717
  • 28
  • 128
  • 190
陆一凡
  • 137
  • 1
  • 11

1 Answers1

1

There are security sandboxes in every browsers, where javascript context cannot interfere. Local filesystem is one of those, so there is no way you can force a file to be saved to a custom folder

Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43