当我在 Windows 系统中下载时,csv 文件日语字符变得垃圾 [英] 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>
控制器
$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'}];
在 Ubuntu 或 Mac 中下载 csv 文件时,日文字符看起来不错,而在 windows 系统数据(日文数据)中下载则是垃圾
While downloading csv file in Ubuntu or in Mac, Japanese characters looks good, while downloading it in windows system data(Japanese data) gets junk
这是垃圾数据的截图
推荐答案
本期的主要问题是日文字符不支持 UTF-8 编码,因此您必须使用 shift_jis 编码创建文件.
filename = "filename_123.csv"
filename = "filename_123.csv"
CSV.open("#{Rails.root}/public/FOLDER_NAME/#{filename}", "w", encoding: params[:encoding]) do |csv|main_array.each 做 |array|csv<<大批结尾结束
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])
csv = CSV.read("#{Rails.root}/public/FOLDER_NAME/#{filename}",encoding: params[:encoding], headers:true)
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])
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' }"
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' }"
这篇关于当我在 Windows 系统中下载时,csv 文件日语字符变得垃圾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!