2013-12-20 59 views
1

我使用MVCFileUploader https://github.com/marufbd/MvcFileUploaderMvcFileUploader添加/編輯自定義字段

這個作品真的很好在我的MVC項目上傳圖片,並將它們與實體相關聯。

當我嘗試爲每個可編輯的圖像添加一個標題字段時,我無法使其工作。我不確定我是否正確地採用這種方法。我修改了_MvcFileLoad.cshtml中的模板以增加字段。我可以在每次上傳時在控制器中獲得標題輸入,但我不知道如何使用此項目的實施將其發送回視圖。

這是_MvcFileUpload.cshtml,我已經添加autoUpload:真

$('#@(formId)').fileupload('option', { 
    autoUpload: true, 
    maxFileSize: @Model.MaxFileSizeInBytes, 
    resizeMaxWidth: 1920, 
    resizeMaxHeight: 1200, 
    acceptFileTypes: @Model.FileTypes 
}); 

我增加了一個輸入標題

<form id="@formId" action="@Model.UploadUrl" method="POST" enctype="multipart/form-data">   
    <div class="row fileupload-buttonbar">      
     <div class="col-lg-7"> 
      <!-- The fileinput-button span is used to style the file input field as button --> 
      <span class="btn btn-success fileinput-button"> 
       <i class="glyphicon glyphicon-plus"></i> 
       <span>Add files...</span> 
       <input type="file" name="files[]" multiple> 

      </span> 
      Title: <input name="title" required /> 

在每個autoUpload我可以保存標題和其關聯與控制器中的文件和實體相關聯。如果用戶想要,我不能回到視圖來編輯它。我試圖修改模板下載在_MvcFileUpload.cshtml

<!-- The template to display files available for download --> 
<script id="template-download" type="text/x-tmpl"> 
{% for (var i=0, file; file=o.files[i]; i++) { %} 
    <tr class="template-download fade"> 
     <td> 
      <span class="preview"> 
       {% if (file.thumbnailUrl) { %} 
        <a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" data-gallery><img src="{%=file.thumbnailUrl%}"></a> 
       {% } %} 
      </span> 
     </td> 
     <td> 
      <p class="name"> 
       {% if (file.url) { %} 
        <a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" {%=file.thumbnailUrl?'data-gallery':''%}>{%=file.name%}</a> 
       {% } else { %} 
        <span>{%=file.name%}</span> 
       {% } %} 
      </p> 
      {% if (file.error) { %} 
       <div><span class="label label-danger">Error</span> {%=file.error%}</div> 
      {% } %} 
     </td> 
     <td> 
     Title: <input name="title" required /> 
      </td> 
     <td> 
      <span class="size">{%=o.formatFileSize(file.size)%}</span> 
     </td> 
     <button class="btn btn-save" data-type="What to put here?" data-url="What to put here?" > 
        <i class="glyphicon glyphicon-save"></i> 
        <span>Save</span> 
       </button> 
      {% if (file.deleteUrl) { %} 
       <button class="btn btn-danger delete" data-type="{%=file.deleteType%}" data-url="{%=file.deleteUrl%}"{% if (file.deleteWithCredentials) { %} data-xhr-fields='{"withCredentials":true}'{% } %}> 
        <i class="glyphicon glyphicon-trash"></i> 
        <span>Delete</span> 
       </button> 
       <input type="checkbox" name="delete" value="1" class="toggle"> 
      {% } else { %} 
       <button class="btn btn-warning cancel"> 
        <i class="glyphicon glyphicon-ban-circle"></i> 
        <span>Cancel</span> 
       </button> 
      {% } %} 
     </td> 
    </tr> 
{% } %} 
</script> 

下一個問題,我看到的是,如果我拿到冠軍回來編輯如何能在更新的輸入發回?只有UploadFile和DeleteFile方法可以調用。

總之,我試圖修改這個,所以用戶可以上傳多張照片並編輯他們的標題。
MvcFileUploader使用blueimp jQuery-File-Upload。我已經看過那些文檔,有些東西就像我在PHP文檔中所要求的,但是我無法讓它與MVC一起工作。 https://github.com/blueimp/jQuery-File-Upload/wiki/PHP-MySQL-database-integration

回答

0

只要你的控制方法是HttpPost,您可以在(string title)參數添加到方法,它會自動抓取從輸入框中的值。否則,您需要將一個data屬性添加到javascript ajax調用中。

要在標題上傳後編輯標題,應該有不同的頁面。讓他們查看圖像,並在可編輯字段中顯示帶有標題的文本框,然後在那裏提交提交按鈕。您不必再次上傳圖像,只需在表單中提供圖像的ID即可。

+0

我已經能夠使用'string title'參數在控制器方法中獲得標題。它將它帶回並編輯它,這是問題所在。我試圖通過擁有不同的頁面來避免您提供的方法,因爲這是一個額外的步驟。感謝您的快速回答。 – user3123649

+0

沒問題。這真的是最好的解決方案。你真的不想再次上傳文件。它真正需要的只是一個簡單的頁面,或者如果你想變得有趣,你可以做一個jQuery UI的消息框。 – krillgar

+0

這不是我一直在尋找的解決方案,但你可能是對的,這是使用這個插件沒有耗時的工作的最佳解決方案。我正在尋找一種解決方案,用戶可以在同一屏幕上更改圖像和標題。它看起來會讓這個插件做一些它不是用來做的事情。感謝您的意見。我將不得不使用這兩個頁面過程,直到我想出我自己的上傳頁面。 – user3123649

相關問題