locked
Not showing full name of image RRS feed

  • Question

  • User1052662409 posted

    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

    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

    Thursday, March 15, 2018 11:20 AM

Answers

  • User753101303 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 15, 2018 12:48 PM
  • User475983607 posted

    demoninside9

    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.

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 15, 2018 7:02 PM

All replies

  • User475983607 posted

    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.

     

    Thursday, March 15, 2018 11:45 AM
  • User753101303 posted

    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 ?

    Thursday, March 15, 2018 11:48 AM
  • User1052662409 posted

    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

    Thursday, March 15, 2018 12:14 PM
  • User753101303 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 15, 2018 12:48 PM
  • User475983607 posted

    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.

    Rethink the design.  

    Thursday, March 15, 2018 1:41 PM
  • User1052662409 posted

    File properties have a created date attribute. There is no need to add the date to the file name.

    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?

    Thursday, March 15, 2018 3:14 PM
  • User475983607 posted

    demoninside9

    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.

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 15, 2018 7:02 PM