Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Appending to file in arbitrary position

Reply
Thread Tools

Appending to file in arbitrary position

 
 
Tulsi Das
Guest
Posts: n/a
 
      11-27-2006
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/.

 
Reply With Quote
 
 
 
 
Jamey Cribbs
Guest
Posts: n/a
 
      11-27-2006
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

 
Reply With Quote
 
 
 
 
Tulsi Das
Guest
Posts: n/a
 
      11-27-2006
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/.

 
Reply With Quote
 
Austin Ziegler
Guest
Posts: n/a
 
      11-27-2006
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/
*

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Where is Form Relative Position and Absolute Position in VS.Net 2005 ? Luqman ASP .Net 1 02-07-2006 10:27 AM
position image based on document position edouard.lauer@pt.lu Javascript 3 01-14-2006 06:04 PM
calling an arbitrary function w/ arbitrary arguments Honestmath C++ 5 12-13-2004 06:18 AM
How to set position of a web control depending on other control's position at run-time? James Wong ASP .Net Web Controls 4 07-14-2004 10:24 AM
XPath Q: position of given node in arbitrary nodelist Angus McIntyre XML 2 09-04-2003 12:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57