从Webapp MVC显示文件夹浏览器对话框 [英] Showing a folderBrowser Dialog from webapp MVC

查看:130
本文介绍了从Webapp MVC显示文件夹浏览器对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c#和MVC的新手.我有一个MVC应用程序设置,其中webapp在类似浏览器控件的chrome下运行.我在webapp(html)中有一个按钮,用户可以从中选择他想要的文件夹,然后我要它们的实际路径.

Pretty new to c# and MVC. I've got a MVC application setup in which a webapp runs under a chrome like browser control. I have button inside the webapp (html) from which user able to select folders he wants, then I want the actual path of them.

为此,我向我的MVC控制器发出ajax请求,这将打开folderBrowserDialog(System.Windows.Forms),然后返回路径作为ajax响应.当我使用Visual Studio运行应用程序时,一切正常.但是在制作了应用程序的程序包并安装了exe之后,根本没有显示folderBrowserDialog.正确的值为null

For that I am making an ajax request to my MVC controller which will open folderBrowserDialog (System.Windows.Forms) then returning path back as the ajax response. Everything works fine when I run the application with visual studio. But after making the package of the application and installing the exe, the folderBrowserDialog isn't appearing at all. There are no errors thrown the ajax respons, properly with a value null

这是代码(部分代码)

selectFolderGlobal是全局变量

selectFolderGlobal is a global variable

public JObject OpenFolderExplorer()
    {
        try
        {
            Thread fb= new Thread(new ThreadStart(openFileBrowser), 1);
            fb.SetApartmentState(ApartmentState.STA);
            fb.Start();
            fb.Join();
            JObject selectedFolder = new JObject();
            selectedFolder.Add("selectedFolder", selectedFolderGlobal);
            return selectedFolder;
        }
        catch (Exception ex)
        {
            Logger.Log(" Exception: " + ex.Message);
            JObject errorcode = JObject.Parse(mConstants.EXCEPTION);
            return errorcode;
        }
    }


private void openFileBrowser()
    {
        try
        {
            var fbd= new FolderBrowserDialog();
            fbd.ShowNewFolderButton = false;
            DialogResult result = fbd.ShowDialog(new Form() { TopMost = true, WindowState = FormWindowState.Minimized });
            if (result == DialogResult.OK)
            {
                selectedFolderGlobal= fbd.SelectedPath;
            }
        }
        catch (Exception ex)
        {
            Logger.Entry(" Exception: " + ex.Message);
        }
    }

这样的Ajax响应回来

Ajax response comes back like this

{
  "selectedFolder":null
}

任何人都知道为什么只有在创建包之后(使用.exe制作出来)才可能发生这种情况吗?将System.Windows.Forms.dll添加到依赖项中(如果不是,则应该抛出异常,甚至包构建都将失败)

Anybody has a clue why it might be happening only after the package creation ( after making an .exe out of it) ? The System.Windows.Forms.dll is added to the dependancy ( if it wasn't it should've thrown an exeption or even the package build would've been failed)

推荐答案

您正试图在Web服务器上打开浏览文件夹对话框,用户浏览器正在其本地计算机上运行,​​即使此方法用户无法看到您在其上引发的对话框服务器这些是单独的机器.而且Web应用程序仅在Web应用程序根目录下具有文件使用权,您不能使用其他文件夹,并且实际上您不需要它.

You are trying to open browse folder dialog on web server, user browser is running on his local machine, even if this works user cannot see dialog that you raised on server these are separate machines. And web application has the file usage rights only under web application root directory, you cannot use other folders, and really you don't need it.

您必须了解Web应用程序的工作方式,所有c#代码都在服务器上执行,并且该c#代码生成html,css,javascript和其他文件,然后浏览器下载该内容并在本地计算机上使用.浏览器呈现html,执行javascript ...

You have to understand how web applications work, all c# code is executed on server, and that c# code produces html, css, javascript and other files, then browser downloads that content and use it on local machine. Browser renders html, executes javascript...

在本地主机上调试时,您的计算机既是服务器又是客户端,这就是在VS中工作的原因,并且您可能是在本地计算机上以管理员身份运行的.

When you are debugging on local host then your computer is both server and client, that is the reason this working in VS and you are probably runnin it as administrator on local machine.

这篇关于从Webapp MVC显示文件夹浏览器对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆