按钮Ajax脚本无法正常工作

时间:2013-05-09 10:46:46

标签: c# asp.net-mvc file-conversion

我的按钮的AJax脚本转换文件并将其存储在文件夹中无法正常工作我没有在文件夹中找到任何文件。

我的观看代码

`@{
    ViewBag.Title = "About";
    int i = 1;
}

<hgroup class="title">
    <h1>@ViewBag.Title.</h1>
    <h2>@ViewBag.Message</h2>
</hgroup>
<table style="background-color: lightgreen; border: solid 2px black;">
    <tr>
        <td>
            <b>Name</b>
        </td>
        <td>
            <b>Size</b>
        </td>
        <td>
            <b>Preview</b>
        </td>
        <td>
            <b>Read File</b>
        </td>
         <td>
            <b>Convert File</b>
        </td>
    </tr>
    @foreach (var file in Model)
    {    
        <tr>
            <td>
                @file.Name
            </td>
            <td>
                @(file.Size / 1000) KB
            </td>
            <td>
                @(file.extension)
            </td>
            <td>
                <input id="btnreadfile@(i)" name="@file.Name" class='btnClick' type="button" value="Read File"/>
                <textarea  rows="4" cols="50" class="txtArea@(i)" name ="txtArea">@(ViewBag.DataVal)
</textarea>
            </td>
            <td>
                <input id="btnconvertfile" name="@file.Name" class='btnClick2' type="button" value="Convert File"/>

            </td>
        </tr>   
        i++;
    }
</table>

<textarea></textarea>
<aside>
    <h3>Aside Title</h3>
    <p>
        Use this area to provide additional information.
    </p>
    <ul>
        <li>@Html.ActionLink("Home", "Index", "Home")</li>
        <li>@Html.ActionLink("About", "About", "Home")</li>
        <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
    </ul>
</aside>

<script>
    $(".btnClick").click(function () {
        var selectedId = $(this).attr("id").replace("btnreadfile", "");
        $.ajax({
            url: "/Home/ReadTextFile",
            type: "GET",
            data: { fileName: $(this).attr("name") },
            DataType: "text",
            success: function (str) {
                $(".txtArea" + selectedId).val(str);
            },
            error: function (err) {
                alert(err);
            }
        });
    });
</script>
<script>
    $(".btnClick2").click(function () {
        var selectedId = $(this).attr("id").replace("btnConvertfile", "");
        $.ajax({
            url: "/Shared/About",
            type: "POST",
            data: {document: $(this).attr ("name")},
            DataType: "pdf",
            success: function (str) {
                $("~/downloads/" + selectedId).val(str);
            },
            error: function (err) {
                alert(err);
            }
        });
    });
</script>`

我的方法代码

   `public ActionResult About() 
            Document document = new Document();
            try
            {
                PdfWriter.GetInstance(document, new FileStream(Server.MapPath("~/") + "downloads/" + "print.pdf", FileMode.Create));
                document.Open();
                //string body = TextArea1.value;
                List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(
                                  new StringReader(Request["txtArea"]), null);
                for (int k = 0; k < htmlarraylist.Count; k++)
                {
                    document.Add((IElement)htmlarraylist[k]);
                }

                Paragraph mypara = new Paragraph();
                document.Add(mypara);

                document.Close();

               return Redirect("~/downloads/print.pdf");
            }
            catch (Exception ex)
            {
                ViewBag.DataVal = ex.Message;
                return View();
            }

        }
    }
}`

目的是通过单击按钮将文件从textArea转换为pdf文件。我该如何编写脚本?我想我做错了。

0 个答案:

没有答案