2

My question is the same as this one but for c#: PHP, get file name without file extension

Please see the code below:

[HttpGet("{id}")]
public ActionResult<byte[]> Get(Guid id)
{
    return System.IO.File.ReadAllBytes("Pictures\\" + id );
}

The filetype e.g. jpeg, gif etc is unknown. How do I find the file without a file type?

Do I have to insist that all images are submitted in a particular file format?

What is the most elegant way of doing this?

Johnny
  • 8,939
  • 2
  • 28
  • 33
w0051977
  • 15,099
  • 32
  • 152
  • 329

2 Answers2

3

A naive approach it would be to read the filenames in a directory and then read the file that match the one you are looking for.

// You might have to change the location of the directory
var files = Directory.GetFiles(@"Pictures\");

foreach(var file in files)
{
    if(file.Contains(id.ToString()))
    {
         return System.IO.File.ReadAllBytes("Pictures\\"+file);
    }
}

Calling id.ToString() might not be that you are looking for. You should change this appropriately based on the name of your files. E.g. you could have a guid with hyphens 7c7cdab9-0db7-4ad1-93c1-96ddc92aadaa or a guid without hyphens 44e0391b22bb48018aa6aab517a59238. Here you will find all the corresponding formatters, in order you pick the one that fits in your case.

Christos
  • 53,228
  • 8
  • 76
  • 108
  • Thanks. Do you know an approach to find the file that is not naive? – w0051977 Mar 16 '19 at 12:09
  • You are welcome. Nope, had I known, I would have shared it. I characterize it as a naive approach, since this definitely does not seem to me the best approach, since each time you need to read a file, you have to enumerate all the files in a directory, whereas you just need to read one file. – Christos Mar 16 '19 at 12:11
  • 1
    Thanks +1 for the last paragraph. Could you confirm that there is nothing wrong with just calling id.tostring i.e. the way I understand it is that the filename will just contain hyphens. – w0051977 Mar 16 '19 at 17:12
  • I guess I could just store the file on the server without a file extension and always serve jpegs. Is there a flaw with this approach? – w0051977 Mar 16 '19 at 17:47
  • @w0051977 You are welcome. As you can see here https://referencesource.microsoft.com/#mscorlib/system/guid.cs,a6547a472def7796,references, this call returns the guid in "registry" format, with hyphens. – Christos Mar 16 '19 at 21:34
  • @w0051977 If you know that the files are jpegs, I don't see any benefit or at least I am not aware of any benefit of not storing the files with an extension. Furthermore, I would rather say that might be a good practice to request from your users to post images with specific formats (e.g. jpeg, .png). – Christos Mar 16 '19 at 21:35
0

A somewhat better approach than the above would be to use the overload that allows patterns:

var files = Directory.GetFiles("", $"*{id}*") (the asterisks, of course, can be omitted as needed)

You still have the same issues with the exact Guid format, but at least you are not fetching all the file names in the directory.

If you expect to have many files in the directory it is better.

Jaguar
  • 5,929
  • 34
  • 48