<div>I was messing about with WebImage and found that either webImage or the html <input type="file" ...> is fussy about the case of file extensions.</div> <div>Specifically, I wrote a little code to let the user browse the PC's files and select an image file
to upload then have WebImage save it off to the images directory of my website.</div> <div>Turns out that when the filename is "myImage.GIF" the result is a null value from
WebImage.GetImageFromRequest()</div>
<div></div> <div>If the filename is changed to "myImage.gif" everything works fine and the file is, indeed, saved. I haven't tried it with the other extensions, but this could be a possible explanation
if others find inconsistent results from WebImage.GetImageFromRequest() </div> <div></div>
<div> </div>
Thanks, but that was of no help whatsoever in finding out what the case specificity of the filename and its extension is. I had already checked that reference and found nothing suggesting that it will not take filename.GIF
or filename.Gif or filename.GIf, but only filename.gif. A little further testing found the same thing happening with JPG
vs jpg.
Additionally, I cannot tell whether this is a function of the <input type="file" element or the WebImage helper.
So the question still stands - unanswered. I believe it to be potentially significant, since we may not always have complete control over the filenaming of images we want to upload. Right now the only workaround I am aware of would be to go into the source
directory and rename any candidate files before uploading them - not a thing you'd really want to advise your users to do.
<div>It's a bug with the WebImage, or more specifically how it identifies the mime type of the posted image from the name. The best workaround for it at this point would be to try creating the WebImage and catch any exceptions it throws:- </div> <div> </div>
<div>
public static WebImage GetImageFromRequest() {
var request = HttpContext.Current.Request;
if (request.Files.Length == 0) {
return null;
}
try {
var postedFile = request.Files[0];
var image = new WebImage(postedFile.InputStream) {
FileName = postedFile.FileName
};
return image;
} catch {
// The user uploaded a file that wasn't an image or an image format that we don't understand
return null;
}
}
I tried to intercept the filename from the <input type="file" /> but it could not be done.
The suggestion to change the case of the filename is brilliant with one slight catch: one must be able to access the filename. I tried to do so. Below is the code I used.
Trying to get the filename in the IsPost codeblock:
if (IsPost)
{
var image = Request["Image"];
photo = WebImage.GetImageFromRequest();
var hereIsTheFileName=Path.GetFileName(photo.FileName);
the variable image is NULL - that does not seem to be the way to get the filename
the variable photo is non-null( but only if the extension in the filename is lower case
the variable hereIsTheFileName contains the desired filename (but only if the desired filename had a lowercase exctension)
So what am I missing here - can someone please tell me how I can get access to the filename (where the extension is UPPERCASE) to change the extension to lowercase? How do I get access to the filename in this situation?
The WebMatrix version I am using is the only one out there of which I am aware (introduced back in January 2011). Didn't know there were other versions. I have looked all over the WebMatrix UI anbd cannot find a help/about or any other means of identifying
the version.
So what am I missing here - can someone please tell me how I can get access to the filename (where the extension is UPPERCASE) to change the extension to lowercase? How do I get access to the filename in this situation?
You can't. Well, you can access the name of any file that's uploaded, but you can't change its entry in Request.Files because that is read-only. Which means that you can't use the WebImage helper's GetImageFromRequest method as it is currently implemented
if the file name contains an uppercase extension.
The simplest solution is to use pranavkm's alternative GetImageFromRequest method.
rrrsr7205
The WebMatrix version I am using is the only one out there of which I am aware (introduced back in January 2011). Didn't know there were other versions. I have looked all over the WebMatrix UI and cannot find a help/about or any other means of identifying
the version.
There have been a couple of "refreshes" - the most recent one of which came out several days ago (16th May). They contain some bug fixes and performance improvements to the WebMatrix IDE itself. The most recent version of WebMatrix includes an "About" option
on the menu which tells you which release you have.
The bug which is the topic of discussion in this thread exists in the Web Pages Helpers library. Updates and bug fixes to framework code usually only happens in Service Packs or new versions. You will probably have to wait until Web Pages 2 - whenever that
might be.
rrrsr7205
Member
597 Points
167 Posts
WebImage is case sensitive on extensions (GIF vs gif)
May 18, 2011 07:38 PM|LINK
a-rad
Member
398 Points
142 Posts
Re: WebImage is case sensitive on extensions (GIF vs gif)
May 19, 2011 10:31 AM|LINK
Not sure if this will be of assistance but this page explains a bit more bout the WebImage helper - http://msdn.microsoft.com/en-us/library/system.web.helpers.webimage(v=vs.99).aspx
rrrsr7205
Member
597 Points
167 Posts
Re: WebImage is case sensitive on extensions (GIF vs gif)
May 19, 2011 12:42 PM|LINK
Thanks, but that was of no help whatsoever in finding out what the case specificity of the filename and its extension is. I had already checked that reference and found nothing suggesting that it will not take filename.GIF or filename.Gif or filename.GIf, but only filename.gif. A little further testing found the same thing happening with JPG vs jpg.
Additionally, I cannot tell whether this is a function of the <input type="file" element or the WebImage helper.
So the question still stands - unanswered. I believe it to be potentially significant, since we may not always have complete control over the filenaming of images we want to upload. Right now the only workaround I am aware of would be to go into the source directory and rename any candidate files before uploading them - not a thing you'd really want to advise your users to do.
pranavkm
Participant
786 Points
106 Posts
Re: WebImage is case sensitive on extensions (GIF vs gif)
May 19, 2011 04:53 PM|LINK
public static WebImage GetImageFromRequest() { var request = HttpContext.Current.Request; if (request.Files.Length == 0) { return null; } try { var postedFile = request.Files[0]; var image = new WebImage(postedFile.InputStream) { FileName = postedFile.FileName }; return image; } catch { // The user uploaded a file that wasn't an image or an image format that we don't understand return null; } }</div>scngan
Participant
1650 Points
333 Posts
MVP
Re: WebImage is case sensitive on extensions (GIF vs gif)
May 21, 2011 12:34 AM|LINK
another workaround will be convert the filename from uppercase to lowercase....which version of webmatrix you using currently ?
http://blog.scnetstudio.com
rrrsr7205
Member
597 Points
167 Posts
Re: WebImage is case sensitive on extensions (GIF vs gif)
May 21, 2011 04:41 PM|LINK
I tried to intercept the filename from the <input type="file" /> but it could not be done.
The suggestion to change the case of the filename is brilliant with one slight catch: one must be able to access the filename. I tried to do so. Below is the code I used.
The html was:
<form action="" method="post" enctype="multipart/form-data"> <legend> Select the Image </legend> <label for="Image">Image</label> <input type="file" name="Image" style = "width:45em;"/> <br/> <input type="submit" value="Upload from PC" /> </form>Trying to get the filename in the IsPost codeblock:
if (IsPost) { var image = Request["Image"]; photo = WebImage.GetImageFromRequest(); var hereIsTheFileName=Path.GetFileName(photo.FileName);the variable image is NULL - that does not seem to be the way to get the filename
the variable photo is non-null( but only if the extension in the filename is lower case
the variable hereIsTheFileName contains the desired filename (but only if the desired filename had a lowercase exctension)
So what am I missing here - can someone please tell me how I can get access to the filename (where the extension is UPPERCASE) to change the extension to lowercase? How do I get access to the filename in this situation?
The WebMatrix version I am using is the only one out there of which I am aware (introduced back in January 2011). Didn't know there were other versions. I have looked all over the WebMatrix UI anbd cannot find a help/about or any other means of identifying the version.
Mikesdotnetting
All-Star
134609 Points
16900 Posts
Moderator
MVP
Re: WebImage is case sensitive on extensions (GIF vs gif)
May 21, 2011 06:45 PM|LINK
You can't. Well, you can access the name of any file that's uploaded, but you can't change its entry in Request.Files because that is read-only. Which means that you can't use the WebImage helper's GetImageFromRequest method as it is currently implemented if the file name contains an uppercase extension.
The simplest solution is to use pranavkm's alternative GetImageFromRequest method.
There have been a couple of "refreshes" - the most recent one of which came out several days ago (16th May). They contain some bug fixes and performance improvements to the WebMatrix IDE itself. The most recent version of WebMatrix includes an "About" option on the menu which tells you which release you have.
The bug which is the topic of discussion in this thread exists in the Web Pages Helpers library. Updates and bug fixes to framework code usually only happens in Service Packs or new versions. You will probably have to wait until Web Pages 2 - whenever that might be.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
rrrsr7205
Member
597 Points
167 Posts
Re: WebImage is case sensitive on extensions (GIF vs gif)
May 21, 2011 11:43 PM|LINK
Thanks, Mike
Do you know if that bug has been reported?
Mikesdotnetting
All-Star
134609 Points
16900 Posts
Moderator
MVP
Re: WebImage is case sensitive on extensions (GIF vs gif)
May 22, 2011 05:19 AM|LINK
It doesn't appear to be lodged on Connect: https://connect.microsoft.com/webmatrix/Feedback.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter