0

I have the following code to create a file from Base64.

var byteCharacters =atob(content);
var byteNumbers = new Array(byteCharacters.length);
for (var i = 0; i < byteCharacters.length; i++) {
    byteNumbers[i] = byteCharacters.charCodeAt(i);
}

var byteArray = new Uint8Array(byteNumbers);
var blob = new Blob([byteArray]);
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
window.navigator.msSaveOrOpenBlob(blob, 'msSaveBlob_testFile.docx');

The problem is that when I execute the msSaveOrOpenBlob it ask the user for downloading/saving of file, what I want is to automatically save it to user c drive.

I think this can be done using ActiveX in IE but even when I used ActiveX to save docx file although it saved the file but it says file is corrupted, but for simple txt file it works fine.

 var FileOpener = new ActiveXObject("Scripting.FileSystemObject");

 var FilePointer = FileOpener.OpenTextFile("C:\taqi.docx", 2, true);
 FilePointer.WriteLine("some text");
 FilePointer.Close();

even writing simple string to docx file, give me error of invalid file. But if I open the same docx file in notepad than I can see the text.

Now my question is how can I use the blob object to create file silently. the file can be (txt,docx,ppt,pdf)

Any help will be appreciated.

Thanks

mouldycurryness
  • 69
  • 1
  • 11
Muhammad Taqi
  • 305
  • 2
  • 6
  • 19
  • 1
    To me it is obvious that this cannot be done. Just imagine that every website could silently create a files on an user's C drive (I know...., cookies are also files). That would be a hackers dream. No, the user should always be in full control here. – KIKO Software Jul 19 '19 at 07:37
  • But like I said using ActiveX I'm able to save the file, txt files are being saved without any issue, but for docx file even though they are being saved but it says invalid file – Muhammad Taqi Jul 19 '19 at 07:39
  • But then the user has given permission to install the ActiveX control, basically giving you control over their file system. That is a security risk. Microsoft has to [warn it users against using them](https://www.microsoft.com/en-us/security/pc-security/activex.aspx). It's also one of the reasons they are no longer present in MS Edge. – KIKO Software Jul 19 '19 at 07:47

1 Answers1

1

For security purpose browsers don't permit saving files on the user's computer without his permission. Consider using localstorage to save it in the browser instead : https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

Colin Faivre
  • 94
  • 2
  • 2