Click here to Skip to main content
15,436,002 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have ajaxfileupload control , i want to insert the image to the database ,
but i want to insert it throw button , not from the upload complete event ,
so i make the button but there isn't filename property !
any help ?
p.s i use the ajax control because i want to preview it first and it have a javascript code that preview the image

protected void fileUploader_UploadComplete(object sender, AjaxFileUploadEventArgs file)
        // User can save file to File System, database or in session state
        if (file.ContentType.Contains("jpg") || file.ContentType.Contains("gif")
            || file.ContentType.Contains("png") || file.ContentType.Contains("jpeg"))
            Session["fileContentType_" + file.FileId] = file.ContentType;
            Session["fileContents_" + file.FileId] = file.GetContents();

        // Set PostedUrl to preview the uploaded file.         
        file.PostedUrl = string.Format("?preview=1&fileId={0}", file.FileId);   

protected void btnInsert_Click(object sender, EventArgs e)
        //string filePath = "~/_Manage_/upimages/" + file.FileName;
Updated 28-Dec-12 15:12pm
Sergey Alexandrovich Kryukov 28-Dec-12 18:38pm     CRLF
This is not a correct question. Who told you you need to do exactly that? You need to start with what you want to achieve really, forget about how for this moment... —SA
Ahmad Abd-Elghany 28-Dec-12 18:42pm    
what !!!?
Sergey Alexandrovich Kryukov 28-Dec-12 18:51pm     CRLF
That! What part of my comment you did not understand? Don't try to abuse event mechanism, adhere to it. Explain your ultimate goal; and I tell you how to reach it. —SA
Ahmad Abd-Elghany 28-Dec-12 18:57pm    
i corrected my question :)
Zaf Khan 28-Dec-12 21:08pm     CRLF
Note: I forgot to add.... to my solution... The fileupload will let you upload to ANY location you say as long as it is WRITEABLE location... but you would upload it to a SECRET/NON-PUBLIC area as you dont want everyone to drop just any file in your important folders, so its best to nominate an uploads folder and empty it before/after uploads. off course if your server is busy then there may be several uploads happening at once! so then you cant empty it before/after but using a sequential numbering system helps keep track of the files and you can remove the temporary files once the user ok/cancels their uploaded file, and ALSO you would have the file path for preview once uploaded too.

1 solution

I was always under the impression that the fileupload component alowed you to upload a file to a non publically accessible area like an _uploads/uploads folder for example and the file named to anything you wanted like upload243.doc and then once uploaded (and the file extension PRESERVED) and ONLY THEN the user is given the option to OK/Cancel via your button event.

And if the user OK then you already know the file name and location and you can chunk it into your DB as you like...

And if the user choose CANCEL then you can delete it from your servers NON PUBLIC space.

So dont neccessarily think about completing the action in one shot, even though it may be possible.... try to think of it as a two step manouvre.


1- User chooses file and you UPLOAD to TEMPORARY location
2a- User choose OK and you process and then delete the temporary file
2b- User choose CANCEL you delete the temporary file
Share this answer
Ahmad Abd-Elghany 28-Dec-12 21:12pm     CRLF
i just use that control because it have a javascript code that preview the image after uploadcomplete event . so i don't have to write more codes to make preview for the image before insert it into the database or to make a circle of upload it and preview it to an image and after that insert it .
Zaf Khan 28-Dec-12 22:01pm    
My javascript isnt too tough and neither is my ajax, but i'm sure if the upload event completes without errors then the file path and contents fo file (data) would be saved on the server somewhere most likely in your session object.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900