chang the the test of upload file
This commit is contained in:
parent
a0b8002d55
commit
4dbbcd2f55
|
@ -7,9 +7,9 @@ import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -48,7 +48,6 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
import org.springframework.web.bind.annotation.SessionAttributes;
|
import org.springframework.web.bind.annotation.SessionAttributes;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
|
||||||
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
|
@ -296,7 +295,7 @@ public class ScriptActionController {
|
||||||
public BaseResponseModel uploadScript(
|
public BaseResponseModel uploadScript(
|
||||||
@ModelAttribute("accessToken") String accessToken,
|
@ModelAttribute("accessToken") String accessToken,
|
||||||
@RequestParam("script") CommonsMultipartFile script,
|
@RequestParam("script") CommonsMultipartFile script,
|
||||||
@RequestParam String scriptName, List<File> paramFiles)
|
@RequestParam String scriptName)
|
||||||
throws CustomGenericException {
|
throws CustomGenericException {
|
||||||
if (script.isEmpty()) {
|
if (script.isEmpty()) {
|
||||||
return new BaseResponseModel(false, "empty file");
|
return new BaseResponseModel(false, "empty file");
|
||||||
|
@ -308,12 +307,10 @@ public class ScriptActionController {
|
||||||
RunScenarioModel runScenarioModel = (RunScenarioModel) MarshalHelper
|
RunScenarioModel runScenarioModel = (RunScenarioModel) MarshalHelper
|
||||||
.unmarshal(RunScenarioModel.class, scenarioModel);
|
.unmarshal(RunScenarioModel.class, scenarioModel);
|
||||||
|
|
||||||
HttpResponse httpResponse = this.getCommunicateWithMaster()
|
|
||||||
.uploadScriptWithParamFiles(accessToken, scriptName,
|
|
||||||
runScenarioModel, paramFiles);
|
|
||||||
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) MarshalHelper
|
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) MarshalHelper
|
||||||
.tryUnmarshal(OperateScriptServerResponseModel.class,
|
.tryUnmarshal(OperateScriptServerResponseModel.class,
|
||||||
httpResponse.getContent());
|
"");
|
||||||
|
|
||||||
if (operateScriptServerResponseModel.isSuccess())
|
if (operateScriptServerResponseModel.isSuccess())
|
||||||
return new BaseResponseModel(true, (Object) new String(
|
return new BaseResponseModel(true, (Object) new String(
|
||||||
|
@ -394,24 +391,20 @@ public class ScriptActionController {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "uploadEditScript", method = RequestMethod.POST)
|
@RequestMapping(value = "uploadEditedScipt", method = RequestMethod.POST)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public BaseResponseModel uploadEditScript(
|
public BaseResponseModel uploadEditScript(
|
||||||
@ModelAttribute("accessToken") String accessToken,
|
@ModelAttribute("accessToken") String accessToken,
|
||||||
@RequestParam String content, @RequestParam File file)
|
@RequestParam String content, @RequestParam CommonsMultipartFile[] paramFiles )
|
||||||
throws CustomGenericException {
|
throws CustomGenericException {
|
||||||
System.out.println("uploadEditScript");
|
|
||||||
ScenarioModel scenarioModel = null;
|
ScenarioModel scenarioModel = null;
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
List<File> paramFiles=new LinkedList<File>();
|
|
||||||
paramFiles.add(file);
|
|
||||||
try {
|
try {
|
||||||
scenarioModel = gson.fromJson(content, ScenarioModel.class);
|
scenarioModel = gson.fromJson(content, ScenarioModel.class);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Logger.getLogger(ScriptActionController.class).info(
|
Logger.getLogger(ScriptActionController.class).info(
|
||||||
ExceptionLog.getStackTrace(e));
|
ExceptionLog.getStackTrace(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
RunScenarioModel runScenarioModel = scenarioModel.getRunScenarioModel();
|
RunScenarioModel runScenarioModel = scenarioModel.getRunScenarioModel();
|
||||||
List<PageModel> pageModels = runScenarioModel.getPages();
|
List<PageModel> pageModels = runScenarioModel.getPages();
|
||||||
List<UsePluginModel> usePluginModels = runScenarioModel.getUsePlugins();
|
List<UsePluginModel> usePluginModels = runScenarioModel.getUsePlugins();
|
||||||
|
@ -419,12 +412,11 @@ public class ScriptActionController {
|
||||||
if (!validateScenarioModel(pageModels, usePluginModels))
|
if (!validateScenarioModel(pageModels, usePluginModels))
|
||||||
return new BaseResponseModel(false, "invalidate script");
|
return new BaseResponseModel(false, "invalidate script");
|
||||||
try {
|
try {
|
||||||
ObjectXmlExchange.toXml(RunScenarioModel.class,
|
ObjectXmlExchange.toXml(RunScenarioModel.class, runScenarioModel);
|
||||||
runScenarioModel);
|
|
||||||
|
|
||||||
HttpResponse httpResponse = this.getCommunicateWithMaster()
|
HttpResponse httpResponse = this.getCommunicateWithMaster()
|
||||||
.uploadScriptWithParamFiles(accessToken, scriptName,
|
.uploadScriptWithParamFiles(accessToken, scriptName,
|
||||||
runScenarioModel, paramFiles);
|
runScenarioModel, paramFiles);
|
||||||
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) MarshalHelper
|
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) MarshalHelper
|
||||||
.tryUnmarshal(OperateScriptServerResponseModel.class,
|
.tryUnmarshal(OperateScriptServerResponseModel.class,
|
||||||
httpResponse.getContent());
|
httpResponse.getContent());
|
||||||
|
|
|
@ -24,7 +24,7 @@ import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange;
|
||||||
import org.eclipse.jetty.http.HttpStatus;
|
import org.eclipse.jetty.http.HttpStatus;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class CommunicateWithMaster {
|
public class CommunicateWithMaster {
|
||||||
|
@ -77,11 +77,11 @@ public class CommunicateWithMaster {
|
||||||
|
|
||||||
public HttpResponse uploadScriptWithParamFiles(String accessToken,
|
public HttpResponse uploadScriptWithParamFiles(String accessToken,
|
||||||
String scriptName, RunScenarioModel runScenarioModel,
|
String scriptName, RunScenarioModel runScenarioModel,
|
||||||
List<File> paramFiles) {
|
CommonsMultipartFile[] paramFiles) {
|
||||||
String url = masterAddress + "RecordScript/uploadScript/" + scriptName;
|
String url = masterAddress + "RecordScript/uploadScript/" + scriptName;
|
||||||
List<String> stringPart = new LinkedList<String>();
|
List<String> stringPart = new LinkedList<String>();
|
||||||
stringPart.add(MarshalHelper.tryMarshal(runScenarioModel));
|
stringPart.add(MarshalHelper.tryMarshal(runScenarioModel));
|
||||||
return this.getHttpRequester().postFiles(
|
return this.getHttpRequester().postFilesMulti(
|
||||||
makeAccessTockenMap(accessToken), url, "paramFiles[]",
|
makeAccessTockenMap(accessToken), url, "paramFiles[]",
|
||||||
paramFiles, "scenarioModel", stringPart);
|
paramFiles, "scenarioModel", stringPart);
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ function checkPass(password) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$("body").keydown(function(e) {
|
$("body").keydown(function(e) {
|
||||||
var ee = e || window.event;
|
var ee = e || window.event;
|
||||||
if(ee.keyCode == 13){
|
if(ee.keyCode == 13){
|
||||||
|
|
|
@ -260,7 +260,7 @@ function checkLoadConfig() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkUserScenarios() {
|
function checkUserScenarios() {
|
||||||
var input = $("#userScenarios").find("input");
|
var input= $("#userScenarios").find("input");
|
||||||
var boolean = 0;
|
var boolean = 0;
|
||||||
var number = 0;
|
var number = 0;
|
||||||
for ( var i = 0; i < input.length; i++) {
|
for ( var i = 0; i < input.length; i++) {
|
||||||
|
|
|
@ -1,32 +1,8 @@
|
||||||
var files;
|
var files;
|
||||||
var file1;
|
var file1;
|
||||||
var fileList = new Array();
|
var fileList = [];
|
||||||
|
var fileNames = [];
|
||||||
|
|
||||||
test("upload files and other data", function() {
|
|
||||||
uploadFilesAndScript();
|
|
||||||
});
|
|
||||||
|
|
||||||
// function submitFiles() {
|
|
||||||
// var formData = new FormData();
|
|
||||||
// var file1 = $('#id1').files[0];
|
|
||||||
// var file2 = $('#id2').files[0];
|
|
||||||
// var fileList = new Array();
|
|
||||||
// fileList.push(file1);
|
|
||||||
// fileList.push(file2);
|
|
||||||
// formData.append("script", file1.files[0]);
|
|
||||||
// formData.append("scriptName", "testUploadFiles");
|
|
||||||
// formData.append("paramFiles", fileList);
|
|
||||||
// $.ajax({
|
|
||||||
// url : "uploadScript",
|
|
||||||
// type : "POST",
|
|
||||||
// data : formData,
|
|
||||||
// processData : false, // tell jQuery not to process the data
|
|
||||||
// contentType : false, // tell jQuery not to set contentType
|
|
||||||
// success : function(data) {
|
|
||||||
// alert(data);
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
$(function() {
|
$(function() {
|
||||||
$("input[type=submit]").click(function() {
|
$("input[type=submit]").click(function() {
|
||||||
submit();
|
submit();
|
||||||
|
@ -37,16 +13,25 @@ function submit() {
|
||||||
uploadFilesAndScript();
|
uploadFilesAndScript();
|
||||||
}
|
}
|
||||||
function uploadFilesAndScript() {
|
function uploadFilesAndScript() {
|
||||||
var form = document.createElement("form");
|
var formData=new FormData();
|
||||||
$(form).attr("name", "fileUpload");
|
var filesDiv=$("input[type=file]");
|
||||||
$("body").append($(form));
|
for(var i=0;i<filesDiv.length;i++){
|
||||||
$(form).append($("#param"));
|
|
||||||
var formData = new FormData(form);
|
formData.append("paramFiles",filesDiv[i].files[0],filesDiv[i].files[0].name);
|
||||||
formData.append("content", "");
|
}
|
||||||
var oReq = new XMLHttpRequest();
|
|
||||||
oReq.open("POST", "../uploadEditScript");
|
formData.append("content","");
|
||||||
oReq.send(formData);
|
|
||||||
alert(oReq.responseText);
|
$.ajax({
|
||||||
|
url : "/uploadEditedScipt",
|
||||||
|
type : "POST",
|
||||||
|
contentType:false,
|
||||||
|
processData:false,
|
||||||
|
data : formData,
|
||||||
|
success : function(data) {
|
||||||
|
alert(data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
function createFileInput(fileList) {
|
function createFileInput(fileList) {
|
||||||
|
@ -56,20 +41,7 @@ function createFileInput(fileList) {
|
||||||
lable.text("test");
|
lable.text("test");
|
||||||
input.attr("type", "file");
|
input.attr("type", "file");
|
||||||
input.attr("name", "file");
|
input.attr("name", "file");
|
||||||
input.attr("id","param");
|
input.attr("id", "param");
|
||||||
fileContainer.append(lable);
|
fileContainer.append(lable);
|
||||||
fileContainer.append(input);
|
fileContainer.append(input);
|
||||||
input.change(function(event) {
|
|
||||||
$.each(event.target.files, function(index, file) {
|
|
||||||
var reader = new FileReader();
|
|
||||||
reader.onload = function(event) {
|
|
||||||
object = new Object();
|
|
||||||
object.filename = file.name;
|
|
||||||
object.data = event.target.result;
|
|
||||||
fileList.push(object);
|
|
||||||
};
|
|
||||||
reader.readAsText(file);
|
|
||||||
console.log(file.name);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
|
||||||
|
function upload(){
|
||||||
|
var files= document.getElementById("file");
|
||||||
|
var file=files.files[0];
|
||||||
|
// var file=$("#file").files[0];
|
||||||
|
|
||||||
|
console.log("fileName: " + file.name);
|
||||||
|
var url="/upload";
|
||||||
|
$.post(url,{script:file},function(data){
|
||||||
|
alert(data);
|
||||||
|
}," multipart/form-data")
|
||||||
|
};
|
||||||
|
$(function(){
|
||||||
|
$("#upload").click(function(e){
|
||||||
|
upload();
|
||||||
|
});
|
||||||
|
});
|
|
@ -0,0 +1,20 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>upload file</title>
|
||||||
|
<link rel="stylesheet"
|
||||||
|
href="http://code.jquery.com/qunit/qunit-1.14.0.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="qunit"></div>
|
||||||
|
<div id="qunit-fixture"></div>
|
||||||
|
<div>
|
||||||
|
<input type="file" id="file"> <input type="submit"
|
||||||
|
value="Upload" id="upload">
|
||||||
|
</div>
|
||||||
|
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.0.min.js"></script>
|
||||||
|
<script src="http://code.jquery.com/qunit/qunit-1.14.0.js"></script>
|
||||||
|
<script src="js/uploadFile.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue