Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have just used file uploader control to upload file in my application.. it is simple work as i know....but it not working with some browser versions..


It is very serious looks..because it gives error file is not in .pdf format.. because i have checked files only PDF at c# code end..


Plz help me out...i would like to know whether it is problem at browser due to any add-ons or some thing else in operating system is causing this problem....
Posted
Comments
Karthik Harve 7-Aug-14 7:29am    
Can you show us your code (html & C# code) ?
Gaurav Gupta A Tech Guy 7-Aug-14 7:40am    
<br />
<asp:FileUpload ID="fileupMember" runat="server" />
<br />
<br />
<asp:ImageButton ID="upload" OnClick="upload_Click" runat="server" ImageUrl="~/images/save.png"
Height="20" Width="50" OnClientClick="javascript:fillImageUrl()" />






string fFullName = fileupMember.PostedFile.FileName;
int len = fFullName.Length;
string ext = Path.GetExtension(fFullName);
string str = fFullName.Substring(fFullName.LastIndexOf("\\") + 1);
len = str.Length;
string fileN = str.Substring(0, len - ext.Length);

Regex FilenameRegex = null;
FilenameRegex = new Regex("(.*?)\\.(htm|html|asp|aspx|php|jsp|pdf|doc|docx|xls|xlsx|ppt|pptx|HTM|HTML|ASP|ASPX|PHP|JSP|PDF|DOC|DOCX|XLX|XLSX|PPT|PPTX)$", RegexOptions.IgnoreCase);
int index = fileN.IndexOf(".");

if (!FilenameRegex.IsMatch(fFullName) || index != -1)
{
return "";
}

string Photoname = Path.GetFileNameWithoutExtension(fileupMember.PostedFile.FileName);
string fileSize = fileupMember.PostedFile.ContentLength.ToString();
String ImageFileNameMBA = fileupMember.PostedFile.FileName;
HttpPostedFile ImageFileMBA = fileupMember.PostedFile;
Byte[] stu_imageMBA = new Byte[ImageFileMBA.ContentLength];

Stream fs = fileupMember.PostedFile.InputStream;
fs.Read(stu_imageMBA, 0, Convert.ToInt32(fileSize));

fs.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(fs, true);

string firstLine = sr.ReadLine().ToString();

//string firstLine = "JFIF";

if ((firstLine.IndexOf("%PDF") > -1))
{
return;
}

String[] allowedExtensions = { ".pdf" };
String[] allowedMimeType = { "application/pdf" };

for (int i = 0; i < allowedExtensions.Length; i++)
{
if (FileExtension == allowedExtensions[i])
{
FileOK = true;
}
}

if (FileOK == true)
{
for (int i = 0; i < allowedMimeType.Length; i++)
{
if (fileupMember.PostedFile.ContentType == allowedMimeType[i])
{
FileOK1 = true;
}
}
}

if (FileOK == true)
{
fileupMember.PostedFile.SaveAs(Server.MapPath("./filelocation"));
}
Manoj B. Kalla 9-Aug-14 2:23am    
Dear Gaurav,

According to user agent we have to change code or wirte different style of html code.
Thank You.
Gaurav Gupta A Tech Guy 11-Aug-14 6:21am    
Kindly please tell me... how ?

1 solution

In code file you can use condition as following:

C#
if (FileUpload1.PostedFile.ContentType.ToLower() == "application/pdf")
{
  //Your code
}
else
{
  //Display message that pdf file not selected.
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900