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

3
  • 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. Commented Jul 19, 2019 at 7: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 Commented Jul 19, 2019 at 7: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. It's also one of the reasons they are no longer present in MS Edge. Commented Jul 19, 2019 at 7:47

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.