add error handling to uploaded files

This commit is contained in:
Lee Lawlor 2014-09-16 15:32:22 -04:00
parent b3b397311e
commit a21733db9d
2 changed files with 14 additions and 4 deletions

View File

@ -425,7 +425,7 @@ class ChannelsController < ApplicationController
# if no data # if no data
if params[:upload].blank? || params[:upload][:csv].blank? if params[:upload].blank? || params[:upload][:csv].blank?
flash[:error] = t(:upload_no_file) flash[:alert] = t(:upload_no_file)
redirect_to channel_path(channel.id, :anchor => "dataimport") and return redirect_to channel_path(channel.id, :anchor => "dataimport") and return
end end
@ -433,10 +433,19 @@ class ChannelsController < ApplicationController
Time.zone = params[:feed][:time_zone] Time.zone = params[:feed][:time_zone]
Chronic.time_class = Time.zone Chronic.time_class = Time.zone
# make sure uploaded file doesn't cause errors
csv_array = nil
flash_alert = nil
begin
# read data from uploaded file # read data from uploaded file
csv_array = CSV.parse(params[:upload][:csv].read) csv_array = CSV.parse(params[:upload][:csv].read)
rescue CSV::MalformedCSVError
flash_alert = t(:upload_incorrect_format)
end
# if no data read, output error message
if csv_array.nil? || csv_array.blank? if csv_array.nil? || csv_array.blank?
flash[:error] = t(:upload_no_data) flash[:alert] = flash_alert || t(:upload_no_data)
redirect_to channel_path(channel.id, :anchor => "dataimport") and return redirect_to channel_path(channel.id, :anchor => "dataimport") and return
end end

View File

@ -398,6 +398,7 @@ en:
twitter_new_api_key: "Regenerate API Key" twitter_new_api_key: "Regenerate API Key"
twitter_screen_name: "Twitter Account" twitter_screen_name: "Twitter Account"
upload: "Upload" upload: "Upload"
upload_incorrect_format: "Your CSV file is not properly formatted."
upload_select: "Upload a CSV file to import data into this channel" upload_select: "Upload a CSV file to import data into this channel"
uploading: "uploading file..." uploading: "uploading file..."
upload_successful: "Upload successful." upload_successful: "Upload successful."