我如何使用OpenFileDialog在c# mvc

本文关键字:mvc OpenFileDialog 何使用 | 更新日期: 2023-09-27 17:51:16

我的代码在

 @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);
    }

当像下面这样运行时,有一个例外

"当前线程必须设置为单线程公寓(STA)模式才能进行OLE调用。确保Main函数上有STAThreadAttribute标记。只有在进程附加了调试器时才会引发此异常。"

那么我怎么解决这个呢?

我如何使用OpenFileDialog在c# mvc

你不能在MVC web应用中使用openfiledialog。请使用

<input type="file"/>

在cshtml

中引用这个OpenFileDialog