I am in the process of porting most of our asp apps. to asp.NET and have come across one headache. The process uses two forms to upload images, check they are valid image files, check to see if there are already any registed within the database and delete the old ones, then upload the new ones and record the path within the database.

It gets to ContentType validation with the line :-

==================================================
Line 90: if ( upl.Form(item).ContentType = "image/gif" OR upl.Form(item).ContentType = "image/jpeg" OR upl.Form(item).ContentType = "image/pjpeg") then
=================================================


and then blows out with:-

=============================================

Public member 'ContentType' on type 'String' not found.

Exception Details: System.MissingMemberException: Public member 'ContentType' on type 'String' not found.

============================================

Perhaps I have been staring at this too long, but something is not making any sense. It all worked fine under asp (new attributes like isReference have replaced old ones like isObject -and the usual stuff that stops asp from functioning under .NET)

Any ideas would be appreciated!

Dani AI

Generated

Short answer: the object you get from upl.Form(item) is a plain string (a form field), not a file object, so ContentType does not exist on it. That explains the "Public member 'ContentType' on type 'String' not found" error. was on the right track thinking about types and casting, and is also right that checking the extension is a quick check — but both need to be applied in the right place and with safer validation behind them.

First troubleshooting step: print the runtime type and value for the item you think is a file to confirm what you actually have. For example, in C# you can log the full type name to the response or a trace log:

object o = upl.Form(item);
Response.Write(o == null ? "null" : o.GetType().FullName);

If that shows System.String, you are indexing the wrong collection. Use the upload control's files collection (or ASP.NET Request.Files) to get posted files (those objects expose ContentType, FileName, InputStream, etc.), or cast only after you confirm the object really is a file type (SaFile for SoftArtisans).

For robust validation do three things: (1) check the server-side MIME (file.ContentType) and extension; (2) verify the binary signature (magic bytes) before trusting it; (3) optionally try to decode it with an image library to be certain. A tiny example to check JPEG magic bytes:

bool IsJpeg(Stream s) {
  var b = new byte[3];
  s.Read(b,0,3);
  s.Position = 0;
  return b[0]==0xFF && b[1]==0xD8 && b[2]==0xFF;
}

Final notes: casting helps only when the object is actually a file object; extension checks alone are spoofable; and using a proper files collection (or the control vendor's API for SaFile objects) will avoid the "String" problem altogether.

Recommended Answers

All 4 Replies

I suppose "upl" is of type SoftArtisans.Net.FileUp,

so, have you ever tried casting?

Like replacing
[VB]
if ( upl.Form(item).ContentType = "image/gif" OR upl.Form(item).ContentType = "image/jpeg" OR upl.Form(item).ContentType = "image/pjpeg") then

with something like

[C#]
if ( ((SoftArtisans.Net.SaFile)upl.Form(item)).ContentType == "image/gif" || ((SoftArtisans.Net.SaFile)upl.Form(item)).ContentType == "image/jpeg" || ((SoftArtisans.Net.SaFile)upl.Form(item)).ContentType == "image/pjpeg" )
{ ... }

(I'm sorry, I can read most of VB.net but I can't write it. But I figure you will get my point after all.)

(I'm sorry, I can read most of VB.net but I can't write it. But I figure you will get my point after all.)

I have the same problem with C#.

Since posting this I have run into a number of brick walls - the main problem being the way Fileup treats asp and asp.NET in two different ways, so just porting an existing asp app. is not going to work - it was back to basics and then more hassles with Database connections (a ported asp app. will allow OLEDB connections via MS Provider for ODBC connections but pure asp.NET will not) and a host of other. The same old Micro(spit)soft hassle - if you don't use their SQL Server (how did they get away with naming a commercial Database product after a language anyway? - 'nuther soapbox!) then compatability is a joke! We were totally unable to use .NET with a Sybase back end with Framework 1.0 but the later version has improved things - a bit!

you could just check the file extension of the file that is being uploaded

I tried it too but it failed and i think the reason is the difference syntax between the syntax in VB under ASP classic and the C# under the
Asp.Net

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.