Reputation: 25386
I am trying to do a simple file upload from my ASP.NET MVC web app using uploadify. In IE8, it works fine. In Firefox and Chrome, it never seems to post to the controller action. Can someone help me find what I'm doing wrong?
Here's my html:
<input type="file" id="file_upload" name="FileData" />
I am including jquery 1.4.1 and the contents of the current version of uploadify 2.1.4, which itself includes swfobject 2.2.
Here's my script:
$(function () {
$("#file_upload").uploadify({
'uploader': '/Scripts/uploadify.swf',
'script': '/Uploads/UploadFile',
'cancelImg': '/Content/Images/cancel.png',
'auto': true,
'multi': false,
'folder': '/uploads',
onComplete : function() {
alert("complete");
},
onOpen : function() {
alert("open");
},
onError : function (event, id, fileObj, errorObj) {
alert("error: " + errorObj.info);
}
});
});
And here's my controller action:
public string UploadFile(HttpPostedFileBase FileData)
{
// do stuff with the file
}
In Chrome and Firefox, I get a "Error #2038" message, which seems pretty cryptic from what I can find on the google. What am I doing wrong?
Upvotes: 0
Views: 5687
Reputation: 676
Like Chris Farmer said, session is different in the flash request, cookies .ASPXAUTH (or other session cookie) aren't sent in Chrome and Firefox (You can watch this with Fiddler2)
To solve this problem, you can use "scriptData" with uploadify. This is how i proceed :
Add this to your uploadify js :
string scriptDataValues = string.Empty;
if (Request.Cookies != null && Request.Cookies.Length > 0)
{
// Generate scriptData
scriptDataValues = ", 'scriptData' : {";
string[] formatedData = new string[Request.Cookies.Length];
int i = 0;
foreach (HttpCookie cookie in cookies)
{
// Format cookie to scriptData name:value
formatedData[i] = string.Format("\"{0}\":\"{1}\"", cookie.Name, cookie.Value);
i++;
}
// separate all formated cookies with comma
scriptDataValues += string.Join(",", formatedData);
scriptDataValues += "}";
}
// add scriptData to your js script
string yourScript = "<script type=\"text/javascript\">
$(document).ready(function () { $('#file_upload').uploadify({
'uploader' : '/uploadify/uploadify.swf',
'script' : '/uploadify/uploadify.php',
'cancelImg' : '/uploadify/cancel.png',
'folder' : '/uploads'
" + scriptDataValues + "
}); });
</script>"
And in your action in your controller :
[HttpPost]
public ActionResult UploadProductImage(HttpPostedFileBase image, FormCollection collec)
{
Partner partner = null;
if (!string.IsNullOrEmpty(collec[".ASPXAUTH"]))
{
// Get cookie in POST values
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(collec[".ASPXAUTH"]);
if (ticket.Expiration > DateTime.Now)
{
// Authenticated user, upload the file and return url
}
}
return this.Content(string.Empty);
}
Upvotes: 0
Reputation: 1038710
Things to try:
Upvotes: 3