设置文件的内容类型,在 silverlight SaveFileDialog 中

发布于 2024-11-17 14:53:45 字数 99 浏览 4 评论 0原文

正如标题所说;有没有办法在 Silverlight SaveFileDialog 中设置文件的内容类型?我还没有找到办法,如果不可能的话我并不感到惊讶,只是想问问并看看其他人是否知道。

As the title says; Is there any way to set the content type of a file in Silverlight SaveFileDialog? I haven't found a way, and I'm not surprised if it's not possible, just thought I ask and see if anyone else knows for sure.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_蜘蛛 2024-11-24 14:53:45

您要查找的是上传文件的 Mime 类型。我认为这可以在 Silverlight 中使用,而且我不明白为什么不可以,但是我从未在这种情况下使用过它。您需要添加以下内容:

using System.Reflection;
using System.Runtime.InteropServices;

[System.Runtime.InteropServices.DllImport(@"urlmon.dll", CharSet = CharSet.Auto)]
private extern static System.UInt32 FindMimeFromData(
    System.UInt32 pBC,
    [MarshalAs(UnmanagedType.LPStr)] System.String pwzUrl,
    [MarshalAs(UnmanagedType.LPArray)] byte[] pBuffer,
    System.UInt32 cbSize,
    [MarshalAs(UnmanagedType.LPStr)] System.String pwzMimeProposed,
    System.UInt32 dwMimeFlags,
    out System.UInt32 ppwzMimeOut,
    System.UInt32 dwReserverd
);

public void SomeMethod(string fileName)
{
    string mimeType = getMimeFromFile(fileName);

    if (mimeType == "text/plain")
{
        // do something
}
}

private string getMimeFromFile(string filename)
    {
        if (!File.Exists(filename))
            throw new FileNotFoundException(filename + " not found");

        byte[] buffer = new byte[256];
        using (FileStream fs = new FileStream(filename, FileMode.Open))
        {
            if (fs.Length >= 256)
                fs.Read(buffer, 0, 256);
            else
                fs.Read(buffer, 0, (int)fs.Length);
        }
        try
        {
            System.UInt32 mimetype;
            FindMimeFromData(0, null, buffer, 256, null, 0, out mimetype, 0);
            System.IntPtr mimeTypePtr = new IntPtr(mimetype);
            string mime = Marshal.PtrToStringUni(mimeTypePtr);
            Marshal.FreeCoTaskMem(mimeTypePtr);
            return mime;
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

What you are looking for is the Mime type of the file uploaded. I presume this will work in Silverlight and I cant see why not, however I've never used it in that context. You need to add the following:

using System.Reflection;
using System.Runtime.InteropServices;

[System.Runtime.InteropServices.DllImport(@"urlmon.dll", CharSet = CharSet.Auto)]
private extern static System.UInt32 FindMimeFromData(
    System.UInt32 pBC,
    [MarshalAs(UnmanagedType.LPStr)] System.String pwzUrl,
    [MarshalAs(UnmanagedType.LPArray)] byte[] pBuffer,
    System.UInt32 cbSize,
    [MarshalAs(UnmanagedType.LPStr)] System.String pwzMimeProposed,
    System.UInt32 dwMimeFlags,
    out System.UInt32 ppwzMimeOut,
    System.UInt32 dwReserverd
);

public void SomeMethod(string fileName)
{
    string mimeType = getMimeFromFile(fileName);

    if (mimeType == "text/plain")
{
        // do something
}
}

private string getMimeFromFile(string filename)
    {
        if (!File.Exists(filename))
            throw new FileNotFoundException(filename + " not found");

        byte[] buffer = new byte[256];
        using (FileStream fs = new FileStream(filename, FileMode.Open))
        {
            if (fs.Length >= 256)
                fs.Read(buffer, 0, 256);
            else
                fs.Read(buffer, 0, (int)fs.Length);
        }
        try
        {
            System.UInt32 mimetype;
            FindMimeFromData(0, null, buffer, 256, null, 0, out mimetype, 0);
            System.IntPtr mimeTypePtr = new IntPtr(mimetype);
            string mime = Marshal.PtrToStringUni(mimeTypePtr);
            Marshal.FreeCoTaskMem(mimeTypePtr);
            return mime;
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
生生漫 2024-11-24 14:53:45
 this.dialog = new SaveFileDialog();  

            try 
            {  
                this.dialog.DefaultExt = ".txt";  
                this.dialog.Filter = "Text Files|*.txt|Log Files|*.log|All Files|*.*";  
                this.dialog.FilterIndex = 2;  
            }  
            catch ( Exception ex )  
            {  
                this.tblError.Text = "Error configuring SaveFileDialog: " + ex.Message;  
            }  
 this.dialog = new SaveFileDialog();  

            try 
            {  
                this.dialog.DefaultExt = ".txt";  
                this.dialog.Filter = "Text Files|*.txt|Log Files|*.log|All Files|*.*";  
                this.dialog.FilterIndex = 2;  
            }  
            catch ( Exception ex )  
            {  
                this.tblError.Text = "Error configuring SaveFileDialog: " + ex.Message;  
            }  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文