save file with different name !

  • Thread starter Thread starter Jameel
  • Start date Start date
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 !
 
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 !
 
Back
Top