Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
765 views
in Technique[技术] by (71.8m points)

javascript - csv file Japanese characters got junk while I download in windows system

Html

<button class="btn btn-raised btn-inverse btn-sm" charset="shift_jis" add-bom="true" ng-csv="getSampleData" csv-header="['コード','勘定科目名','Main_code']" filename="{{ samplefilename }}.csv" field-separator="{{separator}}" decimal-separator="{{decimalSeparator}}" ><span translate> load.common.sample </span> </button>

Controller

$scope.csv = {
     content: null,
     header: true,
     headerVisible: true,
     separator: ',',
     separatorVisible: true,
     result: null,
     encodingVisible: true,
     uploadButtonLabel: "upload a csv file"
   };

$scope.samplefilename = "Sample Support Code List"

$scope.getSampleData = [{コード: 'code _1',勘定科目名: '勘定科目名',Main_code:'maincode1'},{コード: 'code_2',勘定科目名: 'name_2', Main_code: 'Miancode2'}];

While downloading csv file in Ubuntu or in Mac, Japanese characters looks good, while downloading it in windows system data(Japanese data) gets junk

here is the screenshot of junk data

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In this issue the main problem is that Japanese characters are not supported UTF-8 encoding so you have to create file with shift_jis encoding.

filename = "filename_123.csv"

Here main_array is collection of header's data and all rows data

CSV.open("#{Rails.root}/public/FOLDER_NAME/#{filename}", "w", encoding: params[:encoding]) do |csv| main_array.each do |array| csv << array end end

csv = CSV.read("#{Rails.root}/public/FOLDER_NAME/#{filename}", encoding: params[:encoding])

csv = CSV.read("#{Rails.root}/public/FOLDER_NAME/#{filename}",encoding: params[:encoding], headers:true)

File.write("#{Rails.root}/public/FOLDER_NAME/#{filename}", csv, encoding: params[:encoding])

files << "#{filename}"

send_file "#{Rails.root}/public/FOLDER_NAME/#{filename}", filename:ERB::Util.url_encode("#{filename}") , :type => "application/csv; charset= #{params[:encoding] =='shift_jis' ? 'shift_jis' : 'UTF-8' }"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...