lundi 17 novembre 2014

File upload working, but callback always fails

I started with the code form this example: http://ift.tt/1u3pGqK

And that got a file upload working up to the server end. That is, I can select a file in my Extjs client, submit the form, and the server receives the file and I can print it out line by line which verifies that I did get the file.


However, the client side call is always failing.


Here's the client side code:



Code:



Ext.applyIf(me, {
items: [
{
xtype: 'form',
margins: '5 5 5 5',
region: 'north',
height: 52,
bodyPadding: 10,
title: 'Filters',
standardSubmit: false,
dockedItems: [
{
xtype: 'toolbar',
dock: 'top',
items: [
{
xtype: 'filefield',
itemId: 'uploader',
width: 500,
hideLabel: true,
name: 'file'
},
{
xtype: 'tbseparator'
},
{
xtype: 'button',
handler: function(button, e) {
var form = this.up('form').getForm();
if(form.isValid()){
form.submit({
url: 'http://localhost:8080/ITSCorrectionSvc/upload.action',
waitMsg: 'Uploading and processing your file . . .',
success: function(form, action) {
alert('Success');
},
failure: function(form, action){
alert("Callback Failed : form is " + form + " action is " + action.value);
}
});
}

},



My server side code is quite simple as well :

Code:



@Controller
public class FileUploadController {

@RequestMapping(value = "/upload.action", method = RequestMethod.POST)
@ResponseBody
public void uploadFile(WebRequest request, FileUploadBean uploadItem, BindingResult result, HttpServletResponse response){
String successResult = "false";
if(uploadItem.getFile() != null)
{
String fileName = uploadItem.getFile().getOriginalFilename();
byte[] file = uploadItem.getFile().getBytes();
successResult = "true";
CommonsMultipartFile uploadedFile = uploadItem.getFile();
try {
InputStream io = uploadedFile.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(io));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
out.append(line);
out.append("\n");
}
System.out.println(out.toString()); //Prints the string content read from input stream
reader.close();
} catch (IOException e) {
e.printStackTrace();
}

// Some type of file processing…
System.err.println("——————————————-");
System.err.println("Test upload: " + uploadItem.getFile().getOriginalFilename());
System.err.println("——————————————-");
}
response.setContentType("text/html");
try {
response.getWriter().write("{success:"+successResult+"}");
} catch (IOException e1) {
e1.printStackTrace();
}
return;
}



If I inspect the action inside the failure callback I see that the responseText is

"{success:false,message:"Permission denied to access property 'document'"}"

and a failuretype : "connect"

This usually means that the client can't connect to the server, often because they are on different servers and it's a protection against cross site scripting. But in this case I know that I actually am hitting the server because I can see the file printing out line by line on the server side.


I'm stumped. Does anyone have any ideas why my callback fails?






File upload working, but callback always fails

Aucun commentaire:

Enregistrer un commentaire