얼마 전에 RoR v.3.2.13을 사용하여 웹 애플리케이션을 작성했습니다. 모든 것이 한동안 올바르게 테스트되고 작동했습니다 ... 이제 모든 환경을 rails 4.2.5로 업데이트 할 것입니다! 나는 프레임 워크를 업그레이드하는 데 많은 문제를 발견했고, 웹에서 많은 제안을 찾기 위해 거의 쉽게 해결되었습니다 !!
마지막은 나에게 더 많은 문제를주고있다 ...
음, 일부 데이터베이스 레코드를 업데이트하기 위해 팜 장치에서 서버로 .xml 스트림을 업로드해야합니다 ....
다음은 모바일 터미널이 서버로 보내는 .xml 스트림의 예입니다.
<?xml version="1.0" encoding="utf-8"?>
<readings>
<reading>
<opercode>a001a</opercode>
<barcode>000073680</barcode>
<opid>4</opid>
<datetime>2012-01-22T00:07:34+01:00</datetime>
</reading>
<reading>
<opercode>a001a</opercode>
<barcode>000073679</barcode>
<opid>4</opid>
<datetime>2012-01-22T00:07:38+01:00</datetime>
</reading>
</readings>
보시다시피, 여기에는 각각 4 개의 필드를 포함하는 많은 판독 값이 포함되어 있습니다.
무선 연결이 작동하지 않는 경우 html 양식과 파일 업로드 필드가 포함 된보기를 구축했습니다 ....
컨트롤러는 여기에서 볼 수있는 것과 동일한 작업을 사용하여 스트림을 수신합니다.
.........................
#
# receives the readings stream and passes it to the parser
def upload
#
# different part of code if request is from .html o .xml
respond_to do |format|
# the request is from .html page
format.html do
# checks the uploaded file 'content type'
if params[:readings_file].instance_variable_get(:@content_type) == "text/xml"
# the file is .xml
# identifies the readings operator_id
operator_id = Operator.find_by_code(params[:readings_file].instance_variable_get(:@original_filename)[0..4]).id
# retrieves the number of readings stored into .xml file
original_filename = params[:readings_file].instance_variable_get(:@original_filename).partition(".")[0]
expected_readings = original_filename.partition("-P-")[2].to_i != 0 ? original_filename.partition("-P-")[2].to_i : original_filename.partition("--")[2].to_i
# binds the temporary file for nokogiri parsing
temp_file = File.open(params[:readings_file].instance_variable_get(:@tempfile))
readings_to_be_loaded = Nokogiri::XML(temp_file)
# parses the readings records
result = parse_xml(readings_to_be_loaded, operator_id)
# checks parsing result against expected readings count
if result != expected_readings
message = "WARNING - Inside .xml " + expected_readings.to_s + " records was present, only " + result.to_s + " has been accepted as valid readings!!"
else
message = "OK - .xml stream succesfully parsed!"
end
else
# the file is not .xml
message = "ERROR - Invalid readings file format detected --> 'not xml'!!"
end
logger.debug message
redirect_to readings_path, :flash => {:message => message} and return
end
#
# the request is from .xml stream
format.xml do
if params[:readings].present?
# determines the number of expected readings inside the stream and retrieves the operator code
if params[:readings][:reading][0].nil?
expected_readings = 1
oper_code = params[:readings][:reading][:opercode]
else
expected_readings = params[:readings][:reading].count
oper_code = params[:readings][:reading][0][:opercode]
end
# initializes the good readings counter
readings_count = 0
........
omissis
........
else
# the stream don't contains readings, return the http error code 406 -> 'Not Acceptable'
logger.debug "ERROR - Readings file content is incorrectly formatted!!"
render nothing: true, status: 406 and return
end
end
end
# not .xml request nor html upload
# nothing has been processed, ingores the upload and returns status code 404 -> 'resource not found'
logger.debug "ERROR - Incorrect data stream content"
render :nothing => true, status: 404 and return
end
................
그런 다음 데이터 스트림이 html에서 파일로 업로드되었거나 .xml 스트림으로 직접 업로드 된 경우 작업을 구분하십시오.
이제 html보기를 사용하여 파일을 업로드하면 모두 잘 작동합니다 (레일 3.2 또는 4.2 모두 올바르게 작동하고 판독 값을 구문 분석합니다) ....
터미널에서 콘솔을 사용하여 직접 .xml 업로드 작업을 시뮬레이션하려면 다음 명령 줄을 사용합니다.
curl -v -H "Content-Type: application/xml; charset=utf-8" --data-ascii @a001a-12115-81843-P-5.xml http://localhost:3000/readings/upload.xml
스트림이 .xml 형식을 사용하여 직접 업로드되는 경우 ... rails 3.2는 정상적으로 실행되고 rails 4.2는 "406 Not Acceptable"을 반환합니다 (추출 된 로그 행 참조).
레일 3.2에서 로그 :
Started POST "/readings/upload.xml" for 127.0.0.1 at 2015-01-22 21:32:35 +0100
Processing by ReadingsController#upload as XML
Parameters: {"readings"=>{"reading"=>[{"opercode"=>"a001a", "barcode"=>"000073685", "opid"=>"4", "datetime"=>"2015-01-21T20:18:20+01:00"}, {"opercode"=>"a001a", "barcode"=>"000073683", "opid"=>"4", "datetime"=>"2015-01-21T20:18:24+01:00"}, {"opercode"=>"a001a", "barcode"=>"000073682", "opid"=>"4", "datetime"=>"2015-01-21T20:18:28+01:00"}, {"opercode"=>"a001a", "barcode"=>"000073679", "opid"=>"4", "datetime"=>"2015-01-21T20:18:36+01:00"}, {"opercode"=>"a001a", "barcode"=>"000073683", "opid"=>"4", "datetime"=>"2015-01-21T20:18:41+01:00"}]}}
Rendered text template (0.0ms)
Completed 200 OK in 49ms (Views: 48.2ms | ActiveRecord: 0.0ms)
레일 4.2에서 로그 :
Started POST "/readings/upload.xml" for 127.0.0.1 at 2016-05-27 21:11:38 +0200
Processing by ReadingsController#upload as XML
ERROR - Readings file content is incorrectly formatted!!
Rendered text template (0.0ms)
Completed 406 Not Acceptable in 2ms (Views: 0.2ms | ActiveRecord: 0.0ms)
매개 변수가 컨트롤러 동작에 전혀 도달하지 않는 것 같습니다 !! (이것은 4.2 버전의 프레임 워크에 대한 일종의 제한 때문이라고 생각하지만 용의자를 확인하거나 반박하는 특정 정보를 찾을 수 없습니다)
PLS 참고 : 손바닥 장치를 사용하여 직접 스트림을 보내려고하면 동일한 응답을받습니다 !!
어떤 제안도 잘 받아 들여집니다.
미리 감사드립니다. 프란체스코
내가 해결 했어 ...
다시 테스트하고 테스트합니다 .... 이것은 문제가되지 않았습니다 !!
개발 환경에서 레일을 시작할 때 로컬 호스트 주소에서만 수신하므로 외부 장치와의 애플리케이션 상호 작용을 테스트해야하는 경우 명시 적으로 개발자 PC 공용 IP 주소에서 수신하도록해야합니다. ...
여기에 설명 된대로 수행됩니다. Rails 4.2 개발 서버의 기본 바인딩 IP를 변경하는 방법은 무엇입니까?
이제 데이터가 웹 서비스에 도달하고 xml 스트림이 올바르게 처리됩니다!
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다