aspnet
Lập trình không biên giới 598 bài
| tạo file default.aspx rồi cho cái này vào
<asp:FileUpload ID="file" runat="server" /> <asp:Button ID="btnRun" runat="server" Text="Run" OnClick="btnRun_Click" /> <h1> <asp:Label ID="lbl" runat="server"></asp:Label> </h1>
trong code viết như sau
if (file.PostedFile.FileName != "") { string fname = Guid.NewGuid().ToString("N"); string f_path = Server.MapPath("/" + fname); file.PostedFile.SaveAs(f_path); string mt = Engine.getMimeFromFile(f_path, file.PostedFile.ContentLength, fname); lbl.Text = mt; }
và cái file engine
public class Engine {
[DllImport("urlmon.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = false)] static extern int FindMimeFromData(IntPtr pBC, [MarshalAs(UnmanagedType.LPWStr)] string pwzUrl, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.I1, SizeParamIndex = 3)] byte [] pBuffer, int cbSize, [MarshalAs(UnmanagedType.LPWStr)] string pwzMimeProposed, int dwMimeFlags, out IntPtr ppwzMimeOut, int dwReserved);
public static string getMimeFromFile(string path, int contentLength, string fname) { IntPtr mimeout;
int MaxContent = contentLength; if (MaxContent > 4096) MaxContent = 4096;
byte [] buf = new byte [MaxContent] ;
FileStream fs = new FileStream(path, FileMode.Open); fs.Read(buf, 0, MaxContent); fs.Close(); int result = FindMimeFromData(IntPtr.Zero, fname, buf, MaxContent, null, 0, out mimeout, 0);
if (result != 0) { Marshal.FreeCoTaskMem(mimeout); return ""; }
string mime = Marshal.PtrToStringUni(mimeout); Marshal.FreeCoTaskMem(mimeout);
return mime.ToLower(); } }
có dùng
using System.Runtime.InteropServices;
là ok --- Coding for food http://yenbai.awas.vn http://tknd.vn http://coder.awas.vn http://awas.vn http://bieuquyet.vn http://webhocsinh.com
|