Sasha
Sasha

Reputation: 20854

asp.net mvc and FileName from WebImage

I tried to get filename from uploaded file with WebImage like so:

var imageName = new WebImage(file.InputStream).FileName;

but FileName property always return null

maybe im missing something?

Upvotes: 0

Views: 1273

Answers (4)

Afzaal Ahmad Zeeshan
Afzaal Ahmad Zeeshan

Reputation: 15860

To get the filename of the Uploaded file, you can get the image from the request and then call this function on it.

var imageName = new WebImage(file.InputStream).FileName;

..would be

var image = WebImage.GetImageFromRequest().FileName;

This is the property for the image that was uploaded in the Request.

Upvotes: 2

Mikael Östberg
Mikael Östberg

Reputation: 17166

I was looking through the source of the constructor you are using with Reflector and there is no place they set the file name.

But you can probably get the file name using

var fileName = Request.Files[0].FileName;

Its only this constructor that set the file name property:

public WebImage(string filePath) 
   : this(new HttpContextWrapper(HttpContext.Current), filePath) {}

and of course the private one it uses.

Upvotes: 1

SLaks
SLaks

Reputation: 887777

When you write new WebImage(file.InputStream), you're creating a WebImage object from a raw stream.
You never pass it anything with a name, so the FileName property is null.

You should use the file.FileName property from the HttpPostedFile object.

Upvotes: 1

Bala R
Bala R

Reputation: 108967

I think the FileName property gets set only if you use the WebImage(String filename) constructor. You should be able to get the file name from file object. Other properties such as Height, Width, etc should work fine.

Upvotes: 1

Related Questions