I have created the following form:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:FileUpload ID="file1" runat="server" EnableViewState="False" />
<br /><br />
<asp:Button ID="cmdSubmit" Text="Submit" runat="server" onclick="cmdSubmit_Click" />
<br /><br />
<asp:TextBox ID="txt1" Text="" runat="server" Height="253px" TextMode="MultiLine" Width="630px"></asp:TextBox>
</form>
</body>
</html>
The code behind file (.cs) is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
[System.Runtime.InteropServices.DllImport("urlmon.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode, ExactSpelling = true, SetLastError = false)]
private extern static int FindMimeFromData(IntPtr pBC, [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
string pwzUrl, [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPArray, ArraySubType = System.Runtime.InteropServices.UnmanagedType.I1, SizeParamIndex = 3)]
byte[] pBuffer, int cbSize, [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
string pwzMimeProposed, int dwMimeFlags, ref IntPtr ppwzMimeOut, int dwReserved);
protected void Page_Load(object sender, EventArgs e)
{
}
public static string getMimeFromFile(HttpPostedFile file)
{
IntPtr mimeout = default(IntPtr);
int MaxContent = (int)file.ContentLength;
if (MaxContent > 50960)
{
MaxContent = 50960;
}
byte[] buf = new byte[MaxContent + 1];
file.InputStream.Read(buf, 0, MaxContent);
int result = FindMimeFromData(IntPtr.Zero, file.FileName, buf, MaxContent, null, 0, ref mimeout, 0);
if (result != 0)
{
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(mimeout);
return "";
}
string mime = System.Runtime.InteropServices.Marshal.PtrToStringUni(mimeout);
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(mimeout);
return mime.ToLower();
}
protected void cmdSubmit_Click(object sender, EventArgs e)
{
string RealMimeType = getMimeFromFile(file1.PostedFile);
txt1.Text = RealMimeType.Trim();
}
}
The problem is the code is working fine with mp3 file and other files as well but when I am trying to load a .mpg file 47 MB, it is refreshing the page and showing "This webpage is not available" in Chrome.
Please correct me where I am wrong.
Thanks & regards,
Indrajeet