jquery tabs中实现多文件上传控制

发布于 2024-09-10 11:01:32 字数 97 浏览 3 评论 0原文

我使用 asp.net mvc 2。我有三个 jquery 选项卡。在三个选项卡中的每一个中,我想上传多个文件并保存在服务器上。最好的方法是什么 我也想实现 ajax 基本文件上传

Im using asp.net mvc 2. I have three jquery tabs. In each of three tabs i want to upload multiple files and save on server. what may be the best approach to do this also i want to implement ajax base file upload

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘心安 2024-09-17 11:01:32

这应该有助于 AJAX 文件上传。对于多个文件上传,快速谷歌会显示jquery 多文件插件

This should help with the AJAX file upload. For multiple file uploads, a quick google shows the jquery multifile plugin.

装迷糊 2024-09-17 11:01:32

Uploadify 基于 flash 和 表单插件 功能更强大一些,因为它可以与其他表单元素一起执行操作。有大量的 jquery 插件可以满足您的需求。我建议谷歌搜索“jquery ajax uploads”并尝试向您提供的不同选项,看看哪一个适合您的项目。

编辑

这是我在使用表单插件在文本区域中返回响应时使用的代码。

这是上传操作:

public FileUploadJSONResult Upload()
    {
        FileUploadJSONResult result;

        try
        {
            if (Request.Files.Count > 0)
            {
                // Save uploaded file here
                AttachmentServices attachmentServices = new AttachmentServices();
                IAttachment attachment = attachmentServices.UploadFile(Request.Files[0]);

                // Wrap the data in a textarea as required by the form plugin, but return it using JSON
                result = new FileUploadJSONResult()
                {
                    Data = new
                    {
                        FileName = attachment.FileName,
                        ErrorMessage = string.Empty
                    }
                };
            }
            else
            {
                result = new FileUploadJSONResult
                {
                    Data = new
                    {
                        FileName = string.Empty,
                        ErrorMessage = "No file to upload. Please select a file to upload."
                    }
                };
            }
        }
        catch (Exception e)
        {
            LogError(logger, e, null);

            Exception root = e;
            while ((root.InnerException) != null)
            {
                root = root.InnerException;
            }

            result = new FileUploadJSONResult
            {
                Data = new
                {
                    FileName = string.Empty,
                    ErrorMessage = root.Message
                }
            };
        }

        return result;
    }

然后这是 FileUploadJSONResult

public class FileUploadJSONResult : JsonResult
{  
    /// <summary>
    /// The following explanation of this code is from http://www.malsup.com/jquery/form:
    /// 
    ///  Since it is not possible to upload files using the browser's XMLHttpRequest object, the Form Plugin 
    ///  uses a hidden iframe element to help with the task. This is a common technique, but it has inherent limitations. 
    ///  The iframe element is used as the target of the form's submit operation which means that the server response is 
    ///  written to the iframe. This is fine if the response type is HTML or XML, but doesn't work as well if the 
    ///  response type is script or JSON, both of which often contain characters that need to be repesented using 
    ///  entity references when found in HTML markup.
    ///  To account for the challenges of script and JSON responses, the Form Plugin allows these responses to be 
    ///  embedded in a textarea element and it is recommended that you do so for these response types when used in 
    ///  conjuction with file uploads. Please note, however, that if a file has not been selected by the user for the 
    ///  file input then the request uses normal XHR to submit the form (not an iframe). This puts the burden on your 
    ///  server code to know when to use a textarea and when not to. If you like, you can use the iframe option of the 
    ///  plugin to force it to always use an iframe mode and then your server can always embed the response in a textarea. 
    /// </summary>
    /// <param name="context">Controller context</param>
    public override void ExecuteResult(ControllerContext context)
    {
        this.ContentType = "text/html";
        context.HttpContext.Response.Write("<textarea>");
        base.ExecuteResult(context);
        context.HttpContext.Response.Write("</textarea>");
    }
}

然后这里是启动和处理上传的实际 jquery 调用:

    function BeginFileUpload() {
       // Form plugin options
        var options = {
            success: function(data) { Attachments_ShowResponse(data); },  // post-submit callback 
            dataType: 'json',       // 'xml', 'script', or 'json' (expected server response type) 
            iframe: true
        };

        // Bind the form to the form plugin
        $('#idofForm').ajaxForm(options);
        $('#idofForm').submit();
}

// Callback after a file has been uploaded
function Attachments_ShowResponse(data) {
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 

    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 

    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 

    if (data.ErrorMessage == '') {
        Attachments_CreateNewTableRow(data.FileName);
    } else {
        Attachments_AppendError(settings.controlID, 'Error uploading ' + data.FileName + ': ' + data.ErrorMessage);
    }
}

Uploadify is based on flash and Form Plugin is a little more powerful as it can do things with other form elements. There are a ton of jquery plugins out there that will do what you want. I suggest googling 'jquery ajax uploads' and trying out the different options presented to you to see which on will fit with your project.

EDIT

Here is the code I use when using the form plugin to return the response in a textarea.

Here's the upload action:

public FileUploadJSONResult Upload()
    {
        FileUploadJSONResult result;

        try
        {
            if (Request.Files.Count > 0)
            {
                // Save uploaded file here
                AttachmentServices attachmentServices = new AttachmentServices();
                IAttachment attachment = attachmentServices.UploadFile(Request.Files[0]);

                // Wrap the data in a textarea as required by the form plugin, but return it using JSON
                result = new FileUploadJSONResult()
                {
                    Data = new
                    {
                        FileName = attachment.FileName,
                        ErrorMessage = string.Empty
                    }
                };
            }
            else
            {
                result = new FileUploadJSONResult
                {
                    Data = new
                    {
                        FileName = string.Empty,
                        ErrorMessage = "No file to upload. Please select a file to upload."
                    }
                };
            }
        }
        catch (Exception e)
        {
            LogError(logger, e, null);

            Exception root = e;
            while ((root.InnerException) != null)
            {
                root = root.InnerException;
            }

            result = new FileUploadJSONResult
            {
                Data = new
                {
                    FileName = string.Empty,
                    ErrorMessage = root.Message
                }
            };
        }

        return result;
    }

Then here is the FileUploadJSONResult:

public class FileUploadJSONResult : JsonResult
{  
    /// <summary>
    /// The following explanation of this code is from http://www.malsup.com/jquery/form:
    /// 
    ///  Since it is not possible to upload files using the browser's XMLHttpRequest object, the Form Plugin 
    ///  uses a hidden iframe element to help with the task. This is a common technique, but it has inherent limitations. 
    ///  The iframe element is used as the target of the form's submit operation which means that the server response is 
    ///  written to the iframe. This is fine if the response type is HTML or XML, but doesn't work as well if the 
    ///  response type is script or JSON, both of which often contain characters that need to be repesented using 
    ///  entity references when found in HTML markup.
    ///  To account for the challenges of script and JSON responses, the Form Plugin allows these responses to be 
    ///  embedded in a textarea element and it is recommended that you do so for these response types when used in 
    ///  conjuction with file uploads. Please note, however, that if a file has not been selected by the user for the 
    ///  file input then the request uses normal XHR to submit the form (not an iframe). This puts the burden on your 
    ///  server code to know when to use a textarea and when not to. If you like, you can use the iframe option of the 
    ///  plugin to force it to always use an iframe mode and then your server can always embed the response in a textarea. 
    /// </summary>
    /// <param name="context">Controller context</param>
    public override void ExecuteResult(ControllerContext context)
    {
        this.ContentType = "text/html";
        context.HttpContext.Response.Write("<textarea>");
        base.ExecuteResult(context);
        context.HttpContext.Response.Write("</textarea>");
    }
}

Then here is the actual jquery call to start and handle the upload:

    function BeginFileUpload() {
       // Form plugin options
        var options = {
            success: function(data) { Attachments_ShowResponse(data); },  // post-submit callback 
            dataType: 'json',       // 'xml', 'script', or 'json' (expected server response type) 
            iframe: true
        };

        // Bind the form to the form plugin
        $('#idofForm').ajaxForm(options);
        $('#idofForm').submit();
}

// Callback after a file has been uploaded
function Attachments_ShowResponse(data) {
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 

    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 

    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 

    if (data.ErrorMessage == '') {
        Attachments_CreateNewTableRow(data.FileName);
    } else {
        Attachments_AppendError(settings.controlID, 'Error uploading ' + data.FileName + ': ' + data.ErrorMessage);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文