0

I have a aspx page that alows a user to upload an image for their account. It tosses an error when i try to fill the form out from a android phones browser.

I wrote out the type of file my page says my phone is sending for uploading. Its this ... Application/Octet-Stream

How do i accept this file? I tried this code below with out sucess.

                        var FileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);

                        //FileUpload1.PostedFile.ContentType == 

                        if (FileUpload1.PostedFile.ContentType.ToLower() == "image/jpg" ||
                        FileUpload1.PostedFile.ContentType.ToLower() == "image/jpeg" ||
                        FileUpload1.PostedFile.ContentType.ToLower() == "image/pjpeg" ||
                        FileUpload1.PostedFile.ContentType.ToLower() == "image/gif" ||
                        FileUpload1.PostedFile.ContentType.ToLower() == "image/x-png" ||
                        FileUpload1.PostedFile.ContentType.ToLower() == "image/png" ||
                        FileUpload1.PostedFile.ContentType.ToLower() == "Application/Octet-Stream")


                        {
4

1 回答 1

1

Part 1 to your problem is you are making a poor string comparison. The last comparison compares a lower-cased string to a string that has uppercase letters. Try this:

string[] sTypes = { "image/jpg", "image/jpeg", "image/pjpeg", "image/gif", "image/x-png",
    "image/png", "Application/Octet-Stream" };
bool bIsMatch = false;
foreach (string sType in sTypes)
{
    if (String.Compare(sType, FileUpload1.PostedFile.ContentType, true) == 0)
        bIsMatch = true;
}
if (bIsMatch)
{
    //Allow upload
}

Like I mentioned in your OP comments though, I really don't see the point in the content type filter if you are going to allow application/octet-stream which would allow someone to upload a virus or any other executable and a whole slew of other file types.

于 2012-10-16T16:55:45.763 回答