Click here to Skip to main content
15,898,769 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created the following form:

XML
<%@ 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:

C#
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;
        }
        //if (MaxContent > 4096)
        //{
        //    MaxContent = 4096;
        //}

        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
Posted
Comments
syed shanu 20-Mar-14 2:04am    
Chk this links
http://msdn.microsoft.com/en-us/library/aa479405.aspx
http://stackoverflow.com/questions/15248934/maximum-file-size-allowed-by-asp-net-file-upload-control
http://blogs.msdn.com/b/prashant_upadhyay/archive/2011/07/13/large-file-upload-issue-in-asp-net.aspx
Indrojeet_Bhattacharya 20-Mar-14 14:10pm    
Thanks for the help.

you can try and check path.getfileextension(filepath / fileupload.filename) methods using system.io. this will give you the extension of the URL and you can check with mpg, .mpegv etc....
 
Share this answer
 
Comments
Indrojeet_Bhattacharya 20-Mar-14 14:10pm    
Thanks for the help.
Hi...
See this one may its helpful 2 u!
C#
string filter = Path.GetExtension(fileupload.FileName);
              if (filter == ".avi" || filter == ".3gp" || filter == ".mp4" || filter == ".swf" || filter == ".wmv" || filter == ".mp3")
              {
// Write ur code here!.
}
else{
//
}

Thank u.
 
Share this answer
 
Comments
Indrojeet_Bhattacharya 20-Mar-14 14:10pm    
Thanks for the help.
[no name] 21-Mar-14 0:36am    
U are welcome!

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