Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7685917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:21:34+00:00 2026-05-31T19:21:34+00:00

post ‘/upload’ do unless params[:file] && (tmpfile = params[:file][:tempfile]) && (name = params[:file][:filename]) return

  • 0
post '/upload' do
  unless params[:file] && (tmpfile = params[:file][:tempfile]) && (name = params[:file][:filename])
    return haml(:upload)
  end
  time = Time.now.to_s
  time.gsub!(/\s/, '')
  name = time + name
  while blk = tmpfile.read(65536)
    File.open(File.join(Dir.pwd,"public/uploads", name), "wb") { |f| f.write(tmpfile.read) }
  end
  'success'
end

Everything goes where expected the files just end up being corrupted.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:21:36+00:00Added an answer on May 31, 2026 at 7:21 pm

    This bit looks really funky:

    while blk = tmpfile.read(65536)
        File.open(File.join(Dir.pwd,"public/uploads", name), "wb") { |f| f.write(tmpfile.read) }
    end
    

    I’m guessing you’re trying to read your tempfile a 65536-byte block at a time, and then write those blocks successively to your destination file. But you never write blk, which is the first block you read; you write the rest of the file (tempfile.read) instead. And even if this loop did write blocks like it should, it opens the file anew for each block, overwriting the old contents! Anyway, I suspect you meant something like this:

    File.open(File.join(Dir.pwd,"public/uploads", name), "wb") do |f|
        while(blk = tempfile.read(65536))
            f.write(blk)
        end
    end
    

    That said, if you’ve got the file as a temp file (presumably already on your local file system), maybe all you need to do is move that file? It’ll go way faster if that’s the case – if the source and destination are on the same disk, it’s just a matter of swapping some file system pointers, rather than copying all that data.

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

passing post data using cURL requires that the name of the input. However, I
post (id_post, title) tag (id_tag, name) post_tag (id_post_tag, id_post, id_tag) Lets suppose that id_post
This post may seem overly long for just the short question at the end
POST /upload HTTP/1.1 Host: assets.drop.io User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10)
class Post< ActiveRecord::Base end post_array = Post.first If I want to add some data
// Post model return array( 'categories' => array(self::MANY_MANY, 'Category', 'categories_posts(post_id, category_id)') ); I already
First post - hope I'm doing it right! I have a file, lexicon.plist, containing
post.rb Model after_update :assign_owner def assign_owner self.owner = test end The above method works
This post explains how to put file write lock on regular files. But is
Post to wall from Android application... parameters.putString(app_id, mAppId); parameters.putString(picture, http://fbrell.com/f8.jpg); parameters.putString(name, Facebook Dialog); parameters.putString(caption,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.