Rails File Upload File_field error encoding::undefinedconversionerror

Source: Internet
Author: User

The server uses the Ubuntu12 64bit, the environment is ruby1.9.3+rails3+mysql, the test is on the windows2003.

Upload a ". Gitconfig" file, no problem, upload "Sina Weibo data mining. pdf" error, upload "back.jpg" error.

Here are two pieces of information, which are pasted from "Log/production.log". The above paragraph you are no problem of the log, the following section is the log after the error.

01.Started POST "/posts" for 106.3.102.43 at 2012-10-29 21:16:26 +0800
02.Processing by Postscontroller#create as HTML
Parameters: {"UTF8" => "?", "Authenticity_token" => "qg8au6/vw5zmagzyghjdbm7fszr4mb5ckdjegbieoa4=", "post" = >{"category_id" => "1", "title" => "666666666666", "url" => "6666666", "Picture" =>#<actiondispatch:: http::uploadedfile:0x000000032fb838 @original_filename = ". Gitconfig", @content_type = "Application/octet-stream", @ Headers= "Content-disposition:form-data; Name=\ "Post[picture]\"; Filename=\ ". Gitconfig\" \r\ncontent-type:application/octet-stream\r\n ", @tempfile =#<file:/tmp/ Rackmultipart20121029-2609-1lrmc9o>&gt, "content" => "6666", "tags_attributes" =>{"0" => {"title" => "" }}, "Commit" => "Create Post"}
04.Redirected to Http://42.121.5.68/posts
05.Completed 302 Found in 36ms (activerecord:30.1ms)
01.Started POST "/posts" for 123.114.36.100 at 2012-10-30 08:58:13 +0800
02.Processing by Postscontroller#create as HTML
Parameters: {"UTF8" => "?", "Authenticity_token" => "rrnhcdwydn+ontxxc2lmiehpspjwi5glrs6jlprg1ho=", "post" = >{"category_id" => "1", "title" => "blog Try the latest Magic weapon", "url" => "Post7", "Picture" =>#<actiondispatch::http:: uploadedfile:0x000000030df9a0 @original_filename = "Sina Weibo data Mining program. pdf", @content_type = "Binary/octet-stream", @headers = "Content-disposition:form-data; Name=\ "Post[picture]\"; Filename=\ "\xe6\x96\xb0\xe6\xb5\xaa\xe5\xbe\xae\xe5\x8d\x9a\xe6\x95\xb0\xe6\x8d\xae\xe6 \x8C\x96\xE6\x8E\x98\ Xe6\x96\xb9\xe6\xa1\x88.pdf\ "\r\ncontent-type:binary/octet-stream\r\n", @tempfile =#<file:/tmp/ Rackmultipart20121030-16129-15agvlb>&gt, "content" => "blog Taste > Try the latest Magic weapon", "Tags_attributes" => {"0" =>{" Title "=>" blog Try the latest Magic "}}", "Commit" => "Create Post"}
04.Completed Internal Server Error in 45ms
05.
06.encoding::undefinedconversionerror ("\xe2" from Ascii-8bit to UTF-8):
App/controllers/posts_controller.rb:60:in ' Write '
App/controllers/posts_controller.rb:60:in ' block (2 levels) in Create '
App/controllers/posts_controller.rb:59:in ' Open '
App/controllers/posts_controller.rb:59:in ' block in Create '
App/controllers/posts_controller.rb:56:in ' Create '

After comparison, found that two paragraphs of the picture part of the @content_type is not the same, the success is @content_type = "Application/octet-stream", Failure is @content_type= "binary/ Octet-stream ".

Try to upload a txt file, successful, @content_type part is @content_type = "Text/plain".

OK is this part of the reason, that is, the encoding, so error encoding errors, no encoding conversion defined.

The code for the upload section is as follows

Uploaded_io = params[:p ost][:p icture] 
        if uploaded_io!= nil and Uploaded_io.content_type.match (' image ') 
     
          File.Open (Rails.root.join (' Public ', ' uploads ', uploaded_io.original_filename), ' W ') do 

|f| 
     
            F.write (uploaded_io.read) 
          end 
     
        Else
end

After a search, found a deep reason, the original is binary file, Ruby in the Reading and save time will automatically process the binaries, do not need a special way.

In Windows, however, binary and text files are different, and in binary mode, the end line cannot be escaped as a separate newline, but is saved as a carriage return and a newline. So if you read a binary file, you need to indicate in the open that you read the binary file WB. B is the meaning of binary.

The picture defaults to the binary file processing, so the recruit. In fact, you just need to change the W to WB on it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.