我如何在C#mvc中使用OpenFileDialog

时间:2021-04-13 04:05:04

my Code is below

我的代码如下

 @Ajax.ActionLink("Choose File",
                            "chooseItemView",
                            new { },
                            new AjaxOptions
                            {
                                UpdateTargetId = "replaceDiv",
                                InsertionMode = InsertionMode.Replace,
                                HttpMethod = "GET",
                                //OnBegin = "startPreLoader",
                                OnSuccess = "stopPreLoader",
                                OnFailure = "stopPreLoader"
                            }, new
                            {
                                @id=1,
                                @type="file",
                                @class="btn btn-primary offset-top-2",
                                /*@id=item.GetHashCode().GetHashCode(),
                                 * onclick = "fileUploadFunction('" + item.GetHashCode().GetHashCode() + "')"*/
                                onclick = "fileUploadFunction('" + 1 + "')"
                            })



 public ActionResult chooseItemView()
    {
        /*MessageBox.Show("Hi");*/
        OpenFileDialog openFileDialog=new OpenFileDialog();
        openFileDialog.Multiselect = false;
        openFileDialog.Filter = "txt files (*.txt)|*.txt| DOC files (*.doc)|*.doc";
        openFileDialog.ShowDialog();
        return PartialView("_UploadItemView",null);
    }

there is a exceoption when run this as below

运行时如下所示有一个例外情况

"Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process."

“在进行OLE调用之前,必须将当前线程设置为单线程单元(STA)模式。确保主函数上标记了STAThreadAttribute。只有在调试器附加到进程时才会引发此异常。”

so how can i solve this?

那怎么能解决这个问题呢?

1 个解决方案

#1


2  

you can't use openfiledialog in mvc web application. instead use

你不能在mvc web应用程序中使用openfiledialog。改为使用

<input type="file"/>

refer this OpenFileDialog in cshtml

在cshtml中引用此OpenFileDialog

#1


2  

you can't use openfiledialog in mvc web application. instead use

你不能在mvc web应用程序中使用openfiledialog。改为使用

<input type="file"/>

refer this OpenFileDialog in cshtml

在cshtml中引用此OpenFileDialog