Answered by:
how to get ajaxFileUpload save path from the user

Question
-
User-867857476 posted
hi, Good evening,
i want to get ajaxFileUpload save path from the user and i use bellow code, but txtPath.Text always return Empty
protected void ajaxUpldPic_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e) { string filePath = Server.MapPath("~/Uploads/booksPic/Gallery/"); string fileName = System.IO.Path.GetFileName(e.FileName); folderName = txtPath.Text; System.IO.Directory.CreateDirectory(filePath + folderName); int filesize = e.FileSize/1024; if (filesize > 40) { return; } else { while (System.IO.File.Exists(filePath + folderName + "/" + fileName)) fileName = "1" + fileName; ajaxUpldGallery.SaveAs(filePath + folderName +"/"+ fileName); } }
Sunday, October 6, 2013 12:27 PM
Answers
All replies
-
User-1635195291 posted
Hi MostafaTaghipour,
Please Refer:
http://forums.asp.net/t/1906882.aspx?Ajax+File+Upload+
Hope this helps.
Thanks,
Jatin
Sunday, October 6, 2013 9:50 PM -
Monday, October 7, 2013 12:08 AM
-
User-1171043462 posted
The newer browsers strictly follow W3C rules and as per that you cannot get path of the file on client's machine. Thus ignore that thing and there is no point wasting time in it
Monday, October 7, 2013 1:42 AM -
User-867857476 posted
Let me ask the question the other way, how can i send an additional parameters (txtPath.Text) to ajaxUpldPic_UploadComplete method?
Tuesday, October 8, 2013 8:45 AM -
User926484134 posted
Hi Mostafa,
The older browsers used to provide the path. But not possible with new browsers,
You will never get full path of the file due to security reasons.
These is an update for Security Issues.
Wednesday, October 9, 2013 3:33 AM -
User-867857476 posted
hi sumeetssm,
path was an example, this is my question how can i get textbox.text value in uploadComplete method ,Because we can't access controls in uploadComplete method and textbox.text always return empty.
Wednesday, October 9, 2013 7:20 AM -
User926484134 posted
Greetings Mostafa,
Would you please elaborate which "textbox" you are talking about?
Please post your code if you could, We shall try to provide you a solution.
Thursday, October 10, 2013 3:27 AM -
User-867857476 posted
As I understand, we can.t access to controls or variable in ajaxfileupload or asyncfileupload server side methods (inside updatepanel)
Thursday, October 10, 2013 8:34 AM -
User926484134 posted
No thats not the case,
you can access other controls, for instance, refer this code used previously by me....that shall give you an idea...
protected void AsyncFileUpload1_OnUploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e) { string ErrMsg = ""; string extenstion = ""; string path = ""; extenstion = Path.GetExtension(AsyncFileUpload1.FileName); if (extenstion == ".xlsx" || extenstion == ".xls") { try { AsyncFileUpload1.SaveAs(Server.MapPath("~/Ups/") + AsyncFileUpload1.FileName); path = Server.MapPath("~/Ups/") + AsyncFileUpload1.FileName; LoadExcel(extenstion); File.Delete(path); lblProjectMsg.CssClass = "errLabelno"; lblAsyncMsg.Text = "File Uploaded Successfully"; lblAsyncMsg.CssClass = "errLabelno"; lblProjectMsg.Text = "File Uploaded Successfully"; } catch (Exception ex) { ErrMsg = ex.ToString(); } } }
Friday, October 11, 2013 4:48 AM -
User-867857476 posted
unfortunately it's not work, I even got AsyncFileUpload outside of updatepanel but it's not work
this is my behind code:
public void upldbPic_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e) { try { string filePath = Server.MapPath("~/Uploads/booksPic/"); string fileName = System.IO.Path.GetFileName(e.FileName); string[] validEx = { ".jpg", ".jpeg", ".png", ".gif" }; string Ext = System.IO.Path.GetExtension(e.FileName); if (Array.IndexOf(validEx, Ext.ToLower()) < 0) { ShowMessage("فقط فایل با پسوندهای (PNG,JPG,GIF) پشتیبانی می شود."); return; } if (Int32.Parse(e.FileSize) > 41000) { ShowMessage("حد اکثر حجم فایل 40 کیلو بایت است."); return; } while (System.IO.File.Exists(filePath + fileName)) fileName = "1" + fileName; upldbPic.SaveAs(filePath + fileName); Label17.Text = fileName; } catch (Exception ex) { throw ex; } }
Friday, October 11, 2013 8:05 AM -
User926484134 posted
Mostafa,
Please try the following things and post what you find.
- Refer the link and try to find out if something is missing. http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/AsyncFileUpload/AsyncFileUpload.aspx
- Use IE, press F12 , click on the Scripts tab to see possible script errors and let me know if you find any.
- alternatively you can use the Moz firebug to analyze the same.
Please tell what you conclude from this and then i shall post a full example code if you arent able to find.
<ajaxToolkit:AsyncFileUpload OnClientUploadError="uploadError" OnClientUploadComplete="uploadComplete" runat="server" ID="AsyncFileUpload1" UploaderStyle="Modern" UploadingBackColor="#CCFFFF" ThrobberID="myThrobber" />
"OnClient%" methods are to be defined on client side as scripts.
UploadedComplete method should be your serverside method.
I would like to quote one more thing.
Your page will not refresh by the postback of this method. so you shall have to refresh your code manually if you need to by injecting javascript on
ClientUploadComlete.
Please keep me posted you have any further issues.
Saturday, October 12, 2013 2:50 AM -
User-867857476 posted
hi sumeetssm
I don't know where is the problem, Is it possible that use bellow code onclientuploadcomplete or anything like that for duplicate name
while (System.IO.File.Exists(filePath + folderName + "/" + fileName)) fileName = "1" + fileName;
Saturday, October 12, 2013 2:12 PM -
User926484134 posted
Yes Mostafa,
that is allowed, Refer the following code,
int count=1; StringBuilder NameBuilder = new StringBuilder(); string Name = txtFolderName.Text; if (Name!=String.Empty) //new try { Name = Name.Replace(" ", "");//this is to remove spaces in name, to save further agony! do { NameBuilder.Clear(); NameBuilder.Append(Name); NameBuilder.Append((count++).ToString()); } while (Directory.Exists(Server.MapPath(userDta.getFolder() + "/" + NameBuilder.ToString()))); path = userDta.getFolder() + "/" + NameBuilder.ToString(); Directory.CreateDirectory(Server.MapPath(path)); } catch (Exception ex) { lblProjectMsg.CssClass = "errLabel"; lblProjectMsg.Text = "Some Error Occured, Please try again later"; }
This is for directory, the same you can do for file.
StringBuilder is the proper way to manipulate a string.
The sole purpose of the above code is to check wether a directory already exists and append count in front of the directory name and create another directory.
Monday, October 14, 2013 12:48 AM -
User-867857476 posted
Thanks sumeetssm,
But i mean onClientUploadComplete (client-side), not server-side,
i must get uploaded file name to save in DB , but i can't access uploaded file name in server-side and according to the bellow code i can't access uploaded file name in client-side too
while (System.IO.File.Exists(filePath + folderName + "/" + fileName)) fileName = "1" + fileName;
Monday, October 14, 2013 6:26 AM -
User926484134 posted
Greetings Mostafa,
Oh! ....yes,
i am so sorry i didn,t read it carefully,
But you cannot do the coding on onClientUploadComplete,
anyways,
if you will read the above posts once again, you can definately get the uploaded file name as "AsyncFileUpload1.FileName"
I have done the same in a post posted above in an earlier post.
Please let me know. and please mark as answer what helps you.
Thanks.
Monday, October 14, 2013 6:41 AM -