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

依然范特西╮ 提交于 2020-01-20 07:01:08

问题


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


回答1:


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' }"



来源:https://stackoverflow.com/questions/49686816/csv-file-japanese-characters-got-junk-while-i-download-in-windows-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!