PDA

View Full Version : Problem with file upload (always getting a download)



mannyk
7 Sep 2010, 6:38 AM
Hello,

I am using the form upload field from the demo site in an ASP .NET MVC application.
However, I always get the JSON response as a file download and not as an internal response for my script.
What am I doing wrong?

This is my Script Code:



var importAddressForm = new Ext.FormPanel({
id: 'ImportAddressForm',
border: false,
hideBorders: true,
fileUpload: true,
width: 500,
frame: true,
autoHeight: true,
bodyStyle: 'padding: 10px 10px 0 10px;',
labelWidth: 50,
defaults: {
anchor: '95%',
allowBlank: false,
msgTarget: 'side'
},
items: [{
xtype: 'fileuploadfield',
fieldLabel: 'File',
name: 'fileData',
buttonText: 'Browse'
}],
buttons: [{
text: 'Upload',
handler: function () {
if (importAddressForm.getForm().isValid()) {
importAddressForm.getForm().submit({
url: '/Import/UploadFile',
waitMsg: 'Uploading...',
success: function (fp, o) {
alert("success");
},
failure: function (fp, o) {
alert("error");
}
});
}
}
}]
});

This is my server code:


publicActionResult UploadFile(HttpPostedFileBase fileData)
{
string path = Path.Combine(DataConfiguration.ProjectPath, "Import");
if (fileData != null)
{
fileData.SaveAs(Path.Combine(path + Path.GetFileName(fileData.FileName)));
}
return Json(new { success = true, id = Session.SessionID });
}


I don't have any idea what I am doing wrong, so I would be very thankful for any idea!
Yours,
mannyk

JohnsonWangNz
9 Sep 2010, 9:13 PM
I have done similar in asp.net web form, the response header has to include 'attachment' for browser to treat it as a file download

mannyk
9 Sep 2010, 10:03 PM
I have solved the problem.
It seems that the fileupload does not accept a JSON response of the .NET Json object. Instead, I just returned a string in the format of: {success:true, id:"someId"} and it worked. When I compared the strings in detail I noticed that the .NET object formates the string a bit different (with more blanks and "s).