使用mvc 2和jquery/ajax进行光滑上传

Pat*_*cia 7 ajax jquery file-upload slickupload asp.net-mvc-2

我正试图在jquery ui对话框中进行漂亮的上传工作.我已经把文件上传得很好了,我已经检查了样本,他们最终都重新加载了整个页面.我已经设法做到这一点,所以它没有做它的最后回发来处理文件上传后通过设置AutoPostBackAfterUpload="false"

所以它现在将文件放在服务器上,使用随机guid名称.它得到的反应如下:

{
state : "Complete",
reason : "NotTerminated",
percentComplete : 100.00,
percentCompleteText : "100.00 %",
contentLengthText : "826 KB",
transferredLengthText : "826 KB",
remainingLengthText : "0 bytes",
currentFileName : "Desert.jpg",
currentFileIndex : "1",
timeElapsedText : "1 second",
timeElapsedShortText : "00:01",
timeRemainingText : "",
timeRemainingShortText : "00:00",speedText : "596 KB/sec"
}
Run Code Online (Sandbox Code Playgroud)

所以我需要知道的是:当你将AutoPostBackAfterUpload设置为true时,我如何ajaxly发布自动上传的内容.

这是我的代码:<%Html.BeginForm("OrganizationMemberEditContactSectionChangePhoto","OrganizationMember",FormMethod.Post,New With {.id ="uploadForm",.enctype ="multipart/form-data"})%>

    <kw:SlickUpload ID="SlickUpload1" runat="server" AutoPostBackAfterUpload="false"  UploadFormId="uploadForm" ShowDuringUploadElements="cancelButton" HideDuringUploadElements="uploadButton" MaxFiles="1" AutoUploadOnPostBack="false" ProgressInterval="200">
        <DownlevelSelectorTemplate>
            <input type="file" />
        </DownlevelSelectorTemplate>
        <UplevelSelectorTemplate>
            <input type="button" value="Add File" />
        </UplevelSelectorTemplate>
        <FileTemplate>
            <kw:FileListRemoveLink runat="server">[x] remove</kw:FileListRemoveLink>
            <kw:FileListFileName runat="server" />
            <kw:FileListValidationMessage runat="server" ForeColor="Red" />
        </FileTemplate>
        <ProgressTemplate>
            <table width="99%">
                <tr>
                    <td>
                        Uploading <kw:UploadProgressElement ID="UploadProgressElement1" runat="server" Element="FileCountText" />,
                        <kw:UploadProgressElement ID="UploadProgressElement2" runat="server" Element="ContentLengthText">(calculating)</kw:UploadProgressElement>.
                    </td>
                </tr>
                <tr>
                    <td>
                        Currently uploading:
                        <kw:UploadProgressElement ID="UploadProgressElement3" runat="server" Element="CurrentFileName" />,
                        file <kw:UploadProgressElement ID="UploadProgressElement4" runat="server" Element="CurrentFileIndex">&nbsp;</kw:UploadProgressElement>
                        of
                        <kw:UploadProgressElement ID="UploadProgressElement5" runat="server" Element="FileCount" />.
                    </td>
                </tr>
                <tr>
                    <td>
                        Speed:
                        <kw:UploadProgressElement ID="UploadProgressElement6" runat="server" Element="SpeedText">(calculating)</kw:UploadProgressElement>.
                    </td>
                </tr>
                <tr>
                    <td>
                        About
                        <kw:UploadProgressElement ID="UploadProgressElement7" runat="server" Element="TimeRemainingText">(calculating)</kw:UploadProgressElement> remaining.
                    </td>
                </tr>
                <tr>
                    <td>
                        <div style="border: 1px solid #008800; height: 1.5em; position: relative">
                            <kw:UploadProgressBarElement ID="UploadProgressBarElement1" runat="server" Style="background-color: #00ee00; width: 0; height: 1.5em" />
                            <div style="text-align: center; position: absolute; top: .15em; width: 100%">
                                <kw:UploadProgressElement ID="UploadProgressElement8" runat="server" Element="PercentCompleteText">(calculating)</kw:UploadProgressElement>
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </ProgressTemplate>
    </kw:SlickUpload>
    <p>
        <input type="button" value="Upload" id="uploadButton" />
        <a href="javascript:kw.get('<%=SlickUpload1.ClientID %>').cancel()" id="cancelButton" style="display:none">Cancel</a>
    </p>            
<%Html.EndForm()%>
<script type="text/javascript">
    var theThing;
    var urlToPost = "theUrlThatHandlesThePostBack";
    function v2SetUpPhotoDialog() {

        theThing = kw.get("<%=SlickUpload1.ClientID %>");
        theThing.add_OnUploadEnded(function (status) {
            var data = $('#uploadForm').serialize();
            $.ajax({
                type: 'POST',
                url: urlToPost,
                data: data,
                success: function () {
                    v2RegularNotice('success');
                },
                error: function () {
                    v2RegularNotice('fail');
                }
            });
        });

        $('#uploadButton').live('click', function () {
            theThing = kw.get("<%=SlickUpload1.ClientID %>");
            theThing.submit();
            return false;
            //  kw.get("<%=SlickUpload1.ClientID %>").submit();
        });
    }
</script>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我尝试让OnUploadEnded将状态作为参数,但它不会将其填充为操作所需的任何有用信息.它目前序列化表单并发送,但它只填充1个字段.kw_uploadId.

控制器动作还没有做任何事情,只是尝试将UploadStatus作为参数.但如果我只是序列化表格,它就是空的.

我确定我错过了一些明显的东西.但我无法弄清楚.我发现文档很难遵循,在这种情况下没有帮助.

谢谢!

Chr*_*nes 2

与帕特里卡合作后,这个问题已经解决。我们还遇到了一些障碍,但基本情况如下:

这里的主要工作是 SlickUpload 设计的限制:添加 SlickUpload 控件后,您无法将其从 DOM 中删除,然后再重新添加。这将在 SlickUpload6 中得到解决,但不幸的是,这是当前版本的限制。为了解决这个问题,我们在选项卡或对话框不可见时隐藏控件,而不是实际删除它。

还有一个 SlickUpload 次要版本 (5.5.9) 添加了 get_UploadId() 方法,以便更轻松地获取当前上传的上传 id。

这段代码(来自上面):

kw_uploadId: document.getElementById("kw_uploadId").value,
Run Code Online (Sandbox Code Playgroud)

变成:

kw_uploadId: theThing.get_UploadId(),
Run Code Online (Sandbox Code Playgroud)

您可以在这里获取最新版本:SlickUpload 5.5.9