Appending to file in arbitrary position

Discussion in 'Ruby' started by Tulsi Das, Nov 27, 2006.

  1. Tulsi Das

    Tulsi Das Guest

    Hi, I'm implementing the server side of a file upload with resume option
    part of a web app.

    The client sends the last position so I have to append to the existing
    file from that position.

    Current code is:

    @uploadfile = @params["uploadfile"]
    content_range = @request.env['HTTP_CONTENT_RANGE']
    filename = @uploadfile.original_filename
    data = @uploadfile.read

    # position to append from
    pos = content_range[/\d+/].to_i

    f = File.open("#{filename}", "a")
    #f.pos = pos
    f.seek(pos)
    f.write(data)
    f.close

    I've tried opening the file with a and a+ modes. I've tried doing
    rewing, setting pos (like the commented line) and seek, and none of them
    works, the new data is always appended at the end of the file.

    Any clues?

    Thanks

    --
    Posted via http://www.ruby-forum.com/.
     
    Tulsi Das, Nov 27, 2006
    #1
    1. Advertising

  2. Tulsi Das

    Jamey Cribbs Guest

    Tulsi Das wrote:
    > Hi, I'm implementing the server side of a file upload with resume option
    > part of a web app.
    >
    > The client sends the last position so I have to append to the existing
    > file from that position.
    >
    > Current code is:
    >
    > @uploadfile = @params["uploadfile"]
    > content_range = @request.env['HTTP_CONTENT_RANGE']
    > filename = @uploadfile.original_filename
    > data = @uploadfile.read
    >
    > # position to append from
    > pos = content_range[/\d+/].to_i
    >
    > f = File.open("#{filename}", "a")
    > #f.pos = pos
    > f.seek(pos)
    > f.write(data)
    > f.close
    >
    > I've tried opening the file with a and a+ modes. I've tried doing
    > rewing, setting pos (like the commented line) and seek, and none of them
    > works, the new data is always appended at the end of the file.
    >


    Open the file in r+ mode. This is read-write mode that starts at the
    beginning of the file and allows you to seek to a specific position.

    Jamey
     
    Jamey Cribbs, Nov 27, 2006
    #2
    1. Advertising

  3. Tulsi Das

    Tulsi Das Guest

    Jamey Cribbs wrote:
    > Open the file in r+ mode. This is read-write mode that starts at the
    > beginning of the file and allows you to seek to a specific position.


    I can't believe it was so easy, I've never even read the "r" section,
    assuming it was read only... thanks a lot, it works great!

    --
    Posted via http://www.ruby-forum.com/.
     
    Tulsi Das, Nov 27, 2006
    #3
  4. On 11/27/06, Tulsi Das <> wrote:
    > Jamey Cribbs wrote:
    > > Open the file in r+ mode. This is read-write mode that starts at the
    > > beginning of the file and allows you to seek to a specific position.

    > I can't believe it was so easy, I've never even read the "r" section,
    > assuming it was read only... thanks a lot, it works great!


    If you want this to be cross-platform, make sure you actually open it
    "rb+" or your code will mysteriously fail on Windows.

    -austin
    --
    Austin Ziegler * * http://www.halostatue.ca/
    * * http://www.halostatue.ca/feed/
    *
     
    Austin Ziegler, Nov 27, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luqman
    Replies:
    1
    Views:
    679
    Luqman
    Feb 7, 2006
  2. Angus McIntyre
    Replies:
    2
    Views:
    598
    Angus McIntyre
    Sep 4, 2003
  3. Honestmath
    Replies:
    5
    Views:
    592
    Honestmath
    Dec 13, 2004
  4. James Wong
    Replies:
    4
    Views:
    536
    James Wong
    Jul 14, 2004
  5. Replies:
    3
    Views:
    200
Loading...

Share This Page