2011-05-13 28 views
0

所以我已經得到了一些收集的數據(包括一個所見即所得的盒子一些HTML),並將其保存到數據庫的形式...飼養形式的國家有一種意見認爲

我現在需要能夠讓用戶從本地文件中抓取WYSIWYG盒子的內容。在提交表單之前,用戶需要能夠在WYSIWYG框中看到內容。

我已經嘗試添加第二個窗體,充當上傳者和控制器操作獲取文件內容,將其添加到模型,然後重定向回到相同的視圖,但我不知道如何保留已在形式1這樣的領域被輸入的數據...

<% using (Html.BeginForm()) {%> 
    <%: Html.ValidationSummary(true) %> 

    <fieldset> 
     <legend>Create a New Mailing here:</legend> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.Mailing.Sender) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.Mailing.Sender)%> 
      <%: Html.ValidationMessageFor(model => model.Mailing.Sender)%> 
     </div> 

     <div class="editor-field"> 
      <%: Html.TextAreaFor(model => model.Mailing.Body, new { @class = "body-editor", @id = "body-editor" })%> 
      <%: Html.ValidationMessageFor(model => model.Mailing.Body)%> 
     </div> 
     <div class="editor-field"> 
      <%: Html.DropDownListFor(model => model.Mailing.Region, Model.Regions)%> 
      <%: Html.ValidationMessageFor(model => model.Mailing.Region)%> 
     </div> 
     <p> 
      <input type="submit" value="Create" id="submitMailing"/> 
     </p> 
    </fieldset> 

<% } %> 
<% using (Html.BeginForm("Upload", "Mail", FormMethod.Post, new { enctype = "multipart/form-data" })) 
    {%> 
    <%: Html.ValidationSummary(true) %> 

    <fieldset> 
     <legend>Load HTML from a file: </legend> 
     <input type="file" id="htmlFile" name="htmlFile" class="upload" /> 
     <input type="submit" value="Engetenate" id="addContent"/> 
    </fieldset> 

<% } %> 
<div> 
    <%: Html.ActionLink("Back to List", "Index") %> 
</div> 

[HttpPost] 
     public ActionResult Upload(MailingViewModel m, HttpPostedFileBase htmlFile) 
     { 
      TryUpdateModel(m); 
      var reader = new StreamReader(htmlFile.InputStream); 
      m.Mailing.Body = reader.ReadToEnd(); 
      return View("CreateMailing",m); 
     } 

對我失去了我的任何意見或更好的方式來這將是輝煌的...

回答

1

解決這個問題的一種方法是在用戶選擇它後立即在後臺上傳文件,並且一旦上傳到服務器上就暫時存儲它。然後你可以使用AJAX來顯示預覽。至於在後臺上傳文件,你可以看看jquery form plugin,它通過生成一個隱藏的iframe來支持AJAX file uploads。並且由於這個AJAX模擬上傳表單的其他字段將不會被修改,也不會重新加載頁面,因此用戶輸入的值將被保留。

你也可以看看這個jQuery File Upload Demo

+0

你可能是對的,但我以任何方式接近它我似乎打擊跨瀏覽器的差異...所以使用第二種形式並通過ajax提交將上傳文件,但json內容響應被視爲文件下載通過Firefox ...並試圖從文件輸入獲取文件路徑以便使用jQuery.twFile來避免表單提交不起作用,因爲瀏覽器隱藏了jscript的文件路徑...希望有某種.serverAndClientOnSameLan () 方法 :) –