uploadify returning values
what i'm trying to achieve is that:
1) I need to upload the picture and display the picture in the same page.
2) Therefore I need to return the ID of the picture saved in the function to retrieve the picture uploaded.(please see onComplete section)
thanks in advance
Uploadify codes:
<script type="text/javascript">
// <![CDATA[
var userId = $('[id$=HiddenFieldUserID]').val();
var childId = $('[id$=HiddenFieldChildID]').val();
var ImageId = $('[id$=HiddenFieldImageID]').val();
$(document).ready(function() {
$('#fileInput').uploadify({
'uploader': 'uploadify/uploadify.swf',
'script': 'Upload.ashx',
'cancelImg': 'uploadify/cancel.png',
'scriptData': { 'userId': userId, 'humanId': humanId },
'auto': true,
'multi': false,
'fileDesc': 'Image Files',
'fileExt': '*.jpg;*.png;*.gif;*.bmp;*.jpeg',
'queueSizeLimit': 90,
'sizeLimit': 4000000,
'buttonText': 'Choose Images',
'folder': '../img/pictures/',
'onAllComplete': function(event, queueID, fileObj, response, data) {
alert("Upload Successful")
location.href("EditProfile.aspx?pictureid=")
}
Upload.ashx.cs
public class Upload : IHttpHandler
{
int userId = 0;
int childId = 0;
public void ProcessRequest(HttpContext Context)
{
userId = Convert.ToInt32(Context.Request["userid"]);
childId = Convert.ToInt32(Context.Request["childId"]);
try
{
HttpPostedFile file = Context.Request.Files["Filedata"];
Global.myDBManager.ConnectionString = ConfigurationManager.ConnectionStrings["SaveAChildTodayConString"].ConnectionString;
Global.myDBManager.DataProviderType = DBMgr.DataProvider.MySql;
Global.myDBManager.Connect();
int id = 1 + Convert.ToInt32(Global.myDBManager.ExecuteScalar("Select picture_id from picture order by picture_id desc limit 1"));
Global.myDBManager.ExecuteScalar("Insert into picture(picture_id,profile_id) VALUE开发者_开发百科('" + id + "', '" + userId + "')");
file.SaveAs("C:\\Users\\Amber\\Desktop\\" + id.ToString() + ".jpg");
Context.Response.Write("1");
}
catch (Exception ex)
{
Context.Response.Write("0");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
Since the new id is calculated in the cs code you could have the same id assigned to two different pictures.
What you want to do is have a table with an auto key so that the new id is assigned at the db level (SQL is very good at handling locks.) Have the DB call return the new id.
i don't know about .net stuff..you are using multi files to false so there is no need onAllcomplete function and also paramaters are wrong these are for onComplete...
var userId = $('[id$=HiddenFieldUserID]').val(); var childId = $('[id$=HiddenFieldChildID]').val(); var ImageId = $('[id$=HiddenFieldImageID]').val(); $(document).ready(function() { $('#fileInput').uploadify({ 'uploader': 'uploadify/uploadify.swf', 'script': 'Upload.ashx', 'cancelImg': 'uploadify/cancel.png', 'scriptData': { 'userId': userId, 'humanId': humanId }, 'auto': true, 'multi': false, 'fileDesc': 'Image Files', 'fileExt': '*.jpg;*.png;*.gif;*.bmp;*.jpeg', 'queueSizeLimit': 90, 'sizeLimit': 4000000, 'buttonText': 'Choose Images', 'folder': '../img/pictures/', 'onComplete': function(event, queueID, fileObj, response, data) { alert(fileObj.filePath); location.href("EditProfile.aspx?pictureid=") }
now check what it alerts are you getting full file path if yes use this in img src attribute , JSON AND latest id is also the solution this one is quick..
精彩评论