Web Analytics Made Easy -
StatCounter Saving file to a local disk ... (picture) - CodingForum

Announcement

Collapse
No announcement yet.

Saving file to a local disk ... (picture)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Saving file to a local disk ... (picture)

    Hi, I hope ur doing fine ......

    I got a question, I read various articles and they deny each other, also, I havent found the solution:

    Is it possible to write a file to a local disk via Java script ?

    And I dont mean to any system files, protected folders, just a picture directly to a local disk and a folder.

    E.g. I am using WinXp -> let's to root of partition d.

    Why: I want to omit annyoing browsers save as windows and store more than one file at a time ...

    Thank U for answer in advance.
    Have a nice day

  • #2
    No, that would be an immense security hole. Browser security features do not allow javascript to read from local files or to save new local files. Only the browser itself can do that.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

    Comment


    • #3
      OK I thought so and most of articles claimed that etc ...

      So:

      1) is there a way to invoke a browser window "save as" (intrested in chrome and ff) ?
      2) is it possible to send more than one file to save ?
      3) can we prompt a filename in the box (e.g. like in VB formants) ?

      I still have only JS on mind ...

      Thank U for any leads or tips ....

      Comment


      • #4
        1) As far as I know, javascript can only invoke a file dialog box for browsing the user's local files (as pertains to an input element with a type set to "file") for submission in a form. I don't believe javascript can ever invoke the save file dialog in FF. There is (or at least was - not sure if it still works) a way to do it in IE using execCommand("SaveAs",...) but that won't work in FF.

        3) People most often use a content-disposition:attachment header for files that they want a user to "save as" and then simply provide a link to that file in the page. With that method you can "suggest" a file name for the file as well and it all works without javascript. You just need to make an adjustment on the server-side to get that off and running.

        There is a tutorial on that here:

        and here:


        If the mod headers option is too broad for what you need (and it easily could be) then you could make a handler page that will serve the files with a $_GET variable. So any link to a "save as" file would look something like this (assuming PHP for the server-side language):

        http://www.example.com/file_server.php?file=some_file_name.extension

        Then, in file_server.php you will want to first be very careful to make sure the requested file is one that you actually want to share (make sure the user is not directory traversing or trying to download PHP files to get your source code and hack your website, for example). In my opinion, the best approach there is probably "whitelisting" files that are allowed to be downloaded. You can store that list in a database for portability and for ease of use. Then you check the requested file name to see if it's on the list. If it is, you send the HTTP header with the "attachment" disposition and print the file contents.

        There is a PHP script for that here:
        Say you want a PHP script that will make the browser to download a file instead of opening it. This is useful for common filetypes that would normally be displayed in a browser, like .html, images, PDFs and .doc files. You can find a lot of scripts that do this on the web but one


        2) If you want to have the user download multiple files in a batch you can have your server write a *.zip file on the fly with PHP also. That way you don't actually have to create the zip file on the server. You just write it directly to the user's connection. The zip file never clutters up your server because it was never written to the disk.

        The one I have used before is called ZipStream:




        So you have a few options here to get almost exactly what you really want out of your page, but simple javascript is not going to be able to do it. I linked you to a whole bunch of PHP solutions because that is what I know. If your server is running ASP or some other server-side language I won't be able to recommend anything in particular, but I would bet that they have their own solutions for that, too. You would just have to do some google searches.
        The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
        See Mediocrity in its Infancy
        It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
        Seek and you shall find... basically:
        validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

        Comment


        • #5
          you can force a "file" download in firefox from a string of content, but there's no way to name it correctly because eg. window.open("data:text/rtf,hello%20world")) has no content-disposition header...

          you can use a funky mime type and tell firefox to always save that mime type when opened, but again, your filenames will be random and end with a ".part" extension.
          you can use a bat file to re-name the data, but then you get a bit complicated because the file name is not passed to the file handler, so you'll have to pack it with the data and strip it before saving the renamed file. whe.

          one way around this is to use a .tar file; a zip-like archive that can be created using js. you would archive your files, and download the tarball using window.open on a dataURL. let me know if you need code examples for this.
          Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

          Comment

          Working...
          X
          😀
          🥰
          🤢
          😎
          😡
          👍
          👎