save file with different name !

J

Jameel

Hello,

uptill now i able to save the files to the server with thier original names, but i want to save the files with different names to avoid duplication of file names. see the following for what i have acheived uptill now :
strFileName = txtFileName.PostedFile.FileName
strFileName = System.IO.Path.GetFileName(strFileName)
'Save Uploaded file to server
strFileNamePath = strFileFolder & strFileName

If Not (txtFileName.PostedFile Is Nothing) Then
Try
txtFileName.PostedFile.SaveAs(strFileNamePath)
Catch ex As Exception
Response.Write("Error Saving File <Br> " & ex.Message.ToString)
Return False
End Try

Please help me ! I would really appreciate if you would write me some code, not only explain me !
 
P

Peter Rilling

You can do two things.

1) You could use something like File.Exists to see if the file already exists, if it does then append a number on the filename, for instance MyFile.txt (1), MyFile.txt (2), etc.
2) You could generate a unique name regardless of what it was originally named using the Guid class. You won't be able to tell what each file is, but you won't have conflicts.

Hello,

uptill now i able to save the files to the server with thier original names, but i want to save the files with different names to avoid duplication of file names. see the following for what i have acheived uptill now :
strFileName = txtFileName.PostedFile.FileName
strFileName = System.IO.Path.GetFileName(strFileName)
'Save Uploaded file to server
strFileNamePath = strFileFolder & strFileName

If Not (txtFileName.PostedFile Is Nothing) Then
Try
txtFileName.PostedFile.SaveAs(strFileNamePath)
Catch ex As Exception
Response.Write("Error Saving File <Br> " & ex.Message.ToString)
Return False
End Try

Please help me ! I would really appreciate if you would write me some code, not only explain me !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top