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 开发者_Python百科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?
Things to try:
- Your controller action should return ActionResult, not string
- Install Fiddler and see what happens under the covers (you will see the HTTP request/response frames and a possible error). Then compare the results between the different browsers to see if something changes.
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);
}
精彩评论