代码之家  ›  专栏  ›  技术社区  ›  Nanz

DNN MVC模块未发回文件

  •  1
  • Nanz  · 技术社区  · 7 年前

    我正在建立一个小的DNN MVC模块,我需要一个用户上传文件,将处理服务器端。

    我甚至简化了它,所以我在模块上只有一个简单的文件输入和提交按钮,但也失败了。

    我不想回到.ascx控件来让它工作。

    看法

    @inherits DotNetNuke.Web.Mvc.Framework.DnnWebViewPage<NM.Modules.FlexEventsCreate.Models.FlexEventViewModel>
    @using DotNetNuke.Web.Mvc.Helpers
    
    <input type="file" id="fileUp"/>
    <input type="submit" id="btnSubmit" />
    

    控制器

    [DnnHandleError]
    public class ItemController : DnnController
    {
        [HttpPost]
        public ActionResult ShowForm(FlexEventViewModel flexEvent)
        {
            if (ModelState.IsValid)
            {
                var file = Request.Files;
    
                if (file.Count != 0)
                {
                    //do something  
                }
    
                //return RedirectToDefaultRoute();
            }
    
            return View(flexEvent);
        }
    }
    

    <form method="post" action="/Test" id="Form" enctype="multipart/form-data">
    
        <!-- Begin Content areas -->
        <div>
            <div class="row">
                <div class="medium-9 columns">
                    <div id="dnn_LeftPane">
                        <div class="DnnModule DnnModule-DnnModule-747">
                            <a name="747"></a>
    
                            <div class="DnnF_Title_h1 SpacingBottom">
                                <h1><span id="dnn_ctr747_dnnTITLE_titleLabel" class="TitleH1"></span>
    
        </h1>
                                <div id="dnn_ctr747_ContentPane">
                                    <!-- Start_Module_747 -->
                                    <div id="dnn_ctr747_ModuleContent">
                                        <div id="dnn_ctr747_ShowForm_Prog" class="RadAjax RadAjax_Default" style="display:none;">
                                            <div class="raDiv">
    
                                            </div>
                                            <div class="raColor raTransp">
    
                                            </div>
                                        </div>
                                        <div class="RadAjaxPanel" id="dnn_ctr747_dnn_ctr747_ShowForm_UPPanel">
                                            <div id="dnn_ctr747_ShowForm_UP">
                                                <!-- 2013.2.717.40 -->
                                                <div id="mvcContainer-747">
    
                                                    <input type="file" id="fileUp">
                                                    <input type="submit" id="btnSubmit">
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    
    </form>
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   Fix It Scotty    7 年前

    我确实在MVC模块中使用 dropzone jquery component -这可能对你有帮助。查看我的样本 Restaurant Menu project

    @using DotNetNuke.Web.Client.ClientResourceManagement
    @{
        ClientResourceManager.RegisterStyleSheet(Dnn.DnnPage, "~/DesktopModules/MVC/DotNetNuclear/RestaurantMenu/Resources/dropzone/css/dropzone.css");
        ClientResourceManager.RegisterScript(Dnn.DnnPage, "~/DesktopModules/MVC/DotNetNuclear/RestaurantMenu/Resources/dropzone/js/dropzone.min.js", 100);
    }
    

    <div id="dZUpload" class="uploadform dropzone no-margin dz-clickable">
          <div class="dz-default dz-message"></div>
    </div>
    

    初始化组件并告诉它可以上载的文件类型和数量:

    $("#dZUpload").dropzone({
        acceptedFiles: "image/jpeg,image/png,image/gif",
        url: '@Url.Action("Upload", "Menu")',
        maxFiles: 1, // Number of files at a time
        maxFilesize: 1, //in MB
        addRemoveLinks: true,
        maxfilesexceeded: function (file) {
            alert('You have uploaded more than 1 Image. Only the first file will be uploaded!');
        },
        success: function (response) {
    
        }
    });
    

    将acceptedFiles更改为您限制的MIMetype(“应用程序/pdf”等)。更改maxFiles以限制一次可以上载的文件数。

    编写MVC控制器操作以响应Dropzone文件上载url。您可以看到,它需要控制器“菜单”(MenuController.Upload)上的操作方法“Upload”:

    public JsonResult Upload()
    {
        string imageUrl = string.Empty;
        string imgPath = Server.MapPath("~/Portals/0/Restaurant/");
        if (!Directory.Exists(imgPath))
        {
            Directory.CreateDirectory(imgPath);
        }
    
        foreach (string s in Request.Files)
        {
            var file = Request.Files[s];
            if (file.ContentLength > 0)
            {
                string fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(imgPath, fileName);
                file.SaveAs(path);
                imageUrl = string.Format("/Portals/0/Restaurant/{0}", fileName);
            }
        }
    
        return Json(new { img = imageUrl, thumb = imageUrl });
    }