But when I try to bind taht image to gridview it is not showing the full string in fileName. Please go through the below code
string[] filePaths = Directory.GetFiles(Server.MapPath("~/tempimages/"));
List<ListItem> files = new List<ListItem>();
for (int i = 1; i <= oDatatable.Rows.Count; i++)
{
string fileName = Path.GetFileName(oDatatable.Rows[i]["Picture"].ToString());
files.Add(new ListItem(fileName, "~/tempimages/" + fileName));
}
GridView1.DataSource = files;
GridView1.DataBind();
The fileName shows only "25 PM_gaurav-chaudhary-5.jpg" instead of "D:\Gaurav\New folder\responsive\tempimages\3/15/2018 4:13:25 PM_gaurav-chaudhary-5.jpg"
Please help
It is our choices, that show what we truly are, far more than our abilities.
You are sure it is correct? I would expect : and / to be invalid inside a file name though it might be just the UI that prevents those characters ? I would suggest to not use those characters even if it works programmatically.
Then the purpose of GetFileName is to return precisely the file name only (ie at best "3/15/2018 4:13:25 PM_gaurav-chaudhary-5.jpg"). Here it fails because of the : and so whatever is before that seems to be taken as a "drive letter".
What is the point of showing the full path on your server ? Usually you avoid showing actual server side locations.
Finally a common approach to avoid collisions is to keep the name inside the db but to generate a unique name for the disk file (for example using the row pk). I assume this why you added a date time to the actual file name ?
I assume this why you added a date time to the actual file name
because every image should have a unique name.
My requirement is that I want to save a image for a particular date and time and later retrieve in my gridview for reporting. And after 1 week those should be deleted, that's wahy I am not saving those in my database instead saving in a root folder.
I am saving the path so that I can easily generate report with images (depending on my sql query )
Thanks
It is our choices, that show what we truly are, far more than our abilities.
Let' start from the beginning. Check the actual file name. The : and / characters shouldn't be valid in a file name (: is for the suffix for a driver letter and / is for command line options).
You shouldn't be able to create a file named this way.
And with this kind of wrong name what you see is expected :
- Path.GetFileName is intended to return just the file name without its path
- all until the : character is considered as a drive letter and so it returns the value you are seeing as a file name
My requirement is that I want to save a image for a particular date and time and later retrieve in my gridview for reporting. And after 1 week those should be deleted, that's wahy I am not saving those in my database instead saving in a root folder.
File properties have a created date attribute. There is no need to add the date to the file name.
Participant
1450 Points
2845 Posts
Not showing full name of image
Mar 15, 2018 11:20 AM|demoninside9|LINK
Hi All,
I have saved image name with location in my database, below is the full string saved in database.
D:\Gaurav\New folder\responsive\tempimages\3/15/2018 4:13:25 PM_gaurav-chaudhary-5.jpg
But when I try to bind taht image to gridview it is not showing the full string in fileName. Please go through the below code
The fileName shows only "25 PM_gaurav-chaudhary-5.jpg" instead of "D:\Gaurav\New folder\responsive\tempimages\3/15/2018 4:13:25 PM_gaurav-chaudhary-5.jpg"
Please help
All-Star
53781 Points
24074 Posts
Re: Not showing full name of image
Mar 15, 2018 11:45 AM|mgebhard|LINK
The file name uses reserved characters.
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
You'll need to rethink the design.
All-Star
48740 Points
18196 Posts
Re: Not showing full name of image
Mar 15, 2018 11:48 AM|PatriceSc|LINK
Hi,
You are sure it is correct? I would expect : and / to be invalid inside a file name though it might be just the UI that prevents those characters ? I would suggest to not use those characters even if it works programmatically.
Then the purpose of GetFileName is to return precisely the file name only (ie at best "3/15/2018 4:13:25 PM_gaurav-chaudhary-5.jpg"). Here it fails because of the : and so whatever is before that seems to be taken as a "drive letter".
What is the point of showing the full path on your server ? Usually you avoid showing actual server side locations.
Finally a common approach to avoid collisions is to keep the name inside the db but to generate a unique name for the disk file (for example using the row pk). I assume this why you added a date time to the actual file name ?
Participant
1450 Points
2845 Posts
Re: Not showing full name of image
Mar 15, 2018 12:14 PM|demoninside9|LINK
because every image should have a unique name.
My requirement is that I want to save a image for a particular date and time and later retrieve in my gridview for reporting. And after 1 week those should be deleted, that's wahy I am not saving those in my database instead saving in a root folder.
I am saving the path so that I can easily generate report with images (depending on my sql query )
Thanks
All-Star
48740 Points
18196 Posts
Re: Not showing full name of image
Mar 15, 2018 12:48 PM|PatriceSc|LINK
Let' start from the beginning. Check the actual file name. The : and / characters shouldn't be valid in a file name (: is for the suffix for a driver letter and / is for command line options).
You shouldn't be able to create a file named this way.
And with this kind of wrong name what you see is expected :
- Path.GetFileName is intended to return just the file name without its path
- all until the : character is considered as a drive letter and so it returns the value you are seeing as a file name
All-Star
53781 Points
24074 Posts
Re: Not showing full name of image
Mar 15, 2018 01:41 PM|mgebhard|LINK
File properties have a created date attribute. There is no need to add the date to the file name.
Rethink the design.
Participant
1450 Points
2845 Posts
Re: Not showing full name of image
Mar 15, 2018 03:14 PM|demoninside9|LINK
How can I make a image name as unique? There is only date and time variables to be added to the file which makes the name unique.
Because there may be already a image with same name which can be replaced while saving in folder.
Could you please suggest?
All-Star
53781 Points
24074 Posts
Re: Not showing full name of image
Mar 15, 2018 07:02 PM|mgebhard|LINK
A GUID is unique.
Or simply format the Date like 20180315_150302 (yyyyMMdd_hhmmss).
https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings