我可以在asp.net mvc的文件上传jQuery中的对话框 [英] Can i upload file in jquery dialog in asp.net mvc

查看:99
本文介绍了我可以在asp.net mvc的文件上传jQuery中的对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在哪个用户与图像一起输入其数据的形式。这种形式将是一个jQuery对话框。我想知道,如果它是可能的,如果我上传图像,然后将其发送到服务器保存后的硬盘上没有松动的对话框中选择打印信息文件上传。如果是的话我怎么可以使用jQuery当作ajaxForm 插件。即时通讯使用asp.net mvc的。

我曾尝试如下:

我的视图

 < D​​IV ID =对话称号=对话框标题的风格=显示:无>
        < D​​IV ID =乔布斯>
        <表ID =frmuploadENCTYPE =的multipart / form-data的方法=后>
            <输入ID =文件1类型=文件名称=文件/>
            <输入ID =Button1的类型=提交值=上传NAME =键/>
            < /表及GT;
        < / DIV>
    < / DIV>

我的JQuery

 函数对话框(ID){
        $(函数(){
            VAR的pH = $(#乔布斯);
                ph.dialog({
                    宽度:700,
                    模式:真实,
                    显示:'滑',
                    closeText:隐藏,
                    拖动:假的,
                    可调整大小:假的
                });
            //});
        });
    }    功能CLIK(ID){
        对话框(ID);
        返回false;
    }
 VAR的选择= {
            URL:/后/ UploadImages
        };        $('#frmupload')。当作ajaxForm(功能(选件){
            //警报(数据);
            警报(感谢您发表评论!);
        });


解决方案

检查出使用AJAX的文件上传(不幸的是,它看起来像没有给定的JavaScript)下面的例子:

http://jquery.malsup.com/form/#file-upload

基本上,你需要在你的选项 IFRAME 属性对象:

  VAR选项= {
    URL:/后/ UploadImages
    IFRAME:真
};

美中不足的是该插件预计包裹在&LT的响应; textarea的>< / textarea的> 标签(因为响应类型必须是HTML或XML)。您可以通过引入自己的类型响应处理一个AJAX文件上传,如博客帖子的这里

 公共类FileUploadJsonResult:JsonResult
{
    公共覆盖无效的ExecuteReuslt(ControllerContext上下文)
    {
        this.ContentType =text / html的;
        context.HttpContext.Response.Write(&所述; textarea的>中);
        base.ExecuteResult(上下文);
        context.HttpContext.Response.Write(&下; / textarea的>中);
    }
}

然后在你的控制器动作:

 返回新FileUploadJsonResult {数据=新{/ * ... * /}};

I have a form in which user inputs its data along with its image. This form will be in a jquery dialog. I want to know if its is possible that if I upload an image and then send it to server for saving on hard drive after that without loosing that dialog print a message your file uploaded. If yes then how i can use jquery ajaxform plugin. Im using asp.net mvc.

What i have tried is as follows

My View

<div id="dialog" title="Dialog Title" style="display: none">
        <div id="Jobs">
        <form id="frmupload" enctype="multipart/form-data" method="post">
            <input id="File1" type="file" name="file" />
            <input id="Button1" type="submit" value="Upload" name="button" /> 
            </form>
        </div>
    </div>

My JQuery

function dialogs(id) {
        $(function () {
            var ph = $("#Jobs");           
                ph.dialog({
                    width: 700,
                    modal: true,
                    show: 'slide',
                    closeText: 'hide',
                    draggable: false,
                    resizable: false
                });
            //});
        });
    }

    function clik(id) {
        dialogs(id);
        return false;
    }
 var options = {
            url: "/Post/UploadImages"            
        };       

        $('#frmupload').ajaxForm(function (options) {
            //alert(data);
            alert("Thank you for your comment!");
        });

解决方案

Check out the following example that uses an AJAX file upload (unfortunately, it looks like the JavaScript isn't given):

http://jquery.malsup.com/form/#file-upload

Basically, you need to include the iframe property in your options object:

var options = {
    url: "/Post/UploadImages",
    iframe: true
}; 

The catch is that the plugin expects a response wrapped in <textarea></textarea> tags (because the response type must be HTML or XML). You can do this conveniently by introducing your own type of response for handling an AJAX file upload, as outlined in the blog post here:

public class FileUploadJsonResult : JsonResult
{
    public override void ExecuteResult(ControllerContext context)
    {
        this.ContentType = "text/html";
        context.HttpContext.Response.Write("<textarea>");
        base.ExecuteResult(context);
        context.HttpContext.Response.Write("</textarea>");
    }
}

And then in your controller action:

return new FileUploadJsonResult { Data = new { /* ... */ } };

这篇关于我可以在asp.net mvc的文件上传jQuery中的对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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