123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- /*******************************************************************************
- * IBM Confidential
- *
- * OCO Source Materials
- *
- * A and PM: PD
- *
- * (c) Copyright IBM Corp. 2014, 2015
- *
- * The source code for this program is not published or otherwise divested of
- * its trade secrets, irrespective of what has been deposited with the U.S.
- * Copyright Office.
- ******************************************************************************/
- define([
- "dojo/_base/declare",
- "dojo/_base/lang",
- "dojo/_base/array",
- "dojox/form/uploader/plugins/HTML5"
- ],function(declare, lang, array, dojoHTML5){
- var HTML5 = declare("pd/widgets/uploader/HTML5", [dojoHTML5], {
-
- upload: function(data){
- var isEdit = data['inSpec'] != null;
-
- this.onBegin(this.getFileList());
- this.uploadWithFormData(data, !isEdit);
- },
-
- uploadWithFormData: function(data, isGovernorCheck){
- //overwrite dojo original one.
- var formData = new FormData();
-
- if (data) {
- for(var key in data) {
- formData.append(key, data[key]);
- }
-
- data.uploadType = this.uploadType;
- }
-
- if (isGovernorCheck) {
- this._filesTMP = this._files;
- } else if (this._filesTMP) {
- this._files = this._filesTMP;
- }
-
- array.some(this._files, function(file){
- formData.append(this.name, isGovernorCheck ? '' : file);
- return true; //only add the 1st file
- }, this);
-
- var xhr = this.createXhr(data, isGovernorCheck);
- if (dojo.isSafari && xhr.overrideMimeType) {
- xhr.overrideMimeType('text/xml');
- }
- xhr.send(formData);
- },
-
- createXhr: function(data, isGovernorCheck){
- //overwrite dojo original one.
- var xhr = new XMLHttpRequest();
- var timer;
- if (!isGovernorCheck) {
- xhr.upload.addEventListener("progress", lang.hitch(this, "_xhrProgress"), false);
- }
- xhr.addEventListener("load", lang.hitch(this, "_xhrProgress"), false);
- xhr.addEventListener("error", lang.hitch(this, function(evt){
- this.onError(evt);
- clearInterval(timer);
- }), false);
- xhr.addEventListener("abort", lang.hitch(this, function(evt){
- this.onAbort(evt);
- clearInterval(timer);
- }), false);
- xhr.onreadystatechange = lang.hitch(this, function(){
- if(xhr.readyState === 4){
- clearInterval(timer);
- try{
- var result = "";
- if (this.handleAs.toLowerCase() == "json"){
- result = JSON.parse(xhr.responseText.replace(/^\{\}&&/,''));
- } else if (this.handleAs.toLowerCase() == "xml"){
- result = xhr.responseXML;
- } else if (this.handleAs.toLowerCase() == "html"){
- result = xhr.response;
- } else {
- result = xhr.responseText;
- }
-
- if (xhr.status == 200) {
- if (isGovernorCheck) {
- var valid = this.onComplete(result, true);
- if (valid) {
- this.uploadWithFormData(data, false);
- }
- } else {
- this.onComplete(result);
- }
- } else {
- this.onError({
- name: xhr.status,
- message: xhr.status + " " + xhr.statusText,
- response: result //use response as the payload
- });
- }
- } catch(err){
- this.onError(err);
- }
- } else {
- console.log(xhr);
- }
- });
-
- xhr.open("POST", this.getUrl());
- xhr.setRequestHeader("Accept","application/" + this.handleAs);
-
- var responseType = "";
- if (this.handleAs.toLowerCase() == "json"){
- responseType = "json";
- } else if (this.handleAs.toLowerCase() == "html"){
- responseType = dojo.isSafari?"text":"document";
- }
- xhr.responseType = responseType;
-
- timer = setInterval(lang.hitch(this, function(){
- try{
- if(typeof(xhr.statusText)){}
- }catch(e){
- clearInterval(timer);
- }
- }),250);
-
- return xhr;
- }
- });
-
- return HTML5;
- });
|