3 Answers, 1 is accepted
0
Hello,
By default the RadAsyncUpload overwrites the file with the same name in the Target folder. To remedy this you can subscribe to the control's FileUploaded event and insert your custom logic for naming the newly added files in its handler. The following code snippet shows how to do that and as an example uses a random generator to generate a number that is added to the name of the new file if another file with the same name exists in the Target folder:
Regards,
Ivan Danchev
Telerik
By default the RadAsyncUpload overwrites the file with the same name in the Target folder. To remedy this you can subscribe to the control's FileUploaded event and insert your custom logic for naming the newly added files in its handler. The following code snippet shows how to do that and as an example uses a random generator to generate a number that is added to the name of the new file if another file with the same name exists in the Target folder:
protected
void
RadAsyncUpload1_FileUploaded(
object
sender, FileUploadedEventArgs e)
{
string
targetFolder =
"UploadTarget"
;
string
targetFileName = Path.Combine(Server.MapPath(targetFolder), e.File.GetName());
if
(!File.Exists(targetFileName))
{
e.File.SaveAs(targetFileName);
}
else
{
Random rand =
new
Random();
string
randomNum = rand.Next(999, 9999).ToString();
e.File.SaveAs(Path.Combine(Server.MapPath(targetFolder), randomNum + e.File.GetName()));
}
}
Regards,
Ivan Danchev
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
0
Suzy
Top achievements
Rank 2
answered on 11 Mar 2015, 08:22 AM
Hi Ivan,
Thank you, I can save the file now with another name. But when I want to know what the saved name was I cannot get it.
The ASyncUpload is used in the ItemCommand of a datagrid. The code below shows how I get the filename, but the filename I get here is the original filename. I need the saved filename to save it to my database record that is created in the ItemCommand.
Any ideas?
Suzy
Thank you, I can save the file now with another name. But when I want to know what the saved name was I cannot get it.
The ASyncUpload is used in the ItemCommand of a datagrid. The code below shows how I get the filename, but the filename I get here is the original filename. I need the saved filename to save it to my database record that is created in the ItemCommand.
foreach (UploadedFile file in postedFile.UploadedFiles)
{
filename = file.FileName;
fileextension = file.GetExtension();
}
Any ideas?
Suzy
0
Hello,
You can store the uploaded file name into a variable:
So in the example above the savedFileName variable, holds the name of the uploaded file with its extension, "3082MyImage.png" for instance.
Regards,
Ivan Danchev
Telerik
You can store the uploaded file name into a variable:
protected
void
RadAsyncUpload1_FileUploaded(
object
sender, FileUploadedEventArgs e)
{
string
targetFolder =
"images"
;
string
targetFileName = Path.Combine(Server.MapPath(targetFolder), e.File.GetName());
if
(!File.Exists(targetFileName))
{
e.File.SaveAs(targetFileName);
}
else
{
Random rand =
new
Random();
string
randomNum = rand.Next(999, 9999).ToString();
e.File.SaveAs(Path.Combine(Server.MapPath(targetFolder), randomNum + e.File.GetName()));
string
savedFileName = randomNum + e.File.GetName();
}
}
So in the example above the savedFileName variable, holds the name of the uploaded file with its extension, "3082MyImage.png" for instance.
Regards,
Ivan Danchev
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.