使用asp.net mvc 2我有三個jQuery選項卡。在三個選項卡中,我希望上傳多個文件並保存在服務器上。什麼可能是最好的辦法做到這一點我也想實現ajax基本文件上傳在jQuery選項卡中實現多個文件上傳控制
回答
Uploadify基於flash,Form Plugin稍微強大一點,因爲它可以處理其他表單元素。有很多jQuery插件可以做你想做的。我建議用谷歌搜索'jquery ajax uploads'並嘗試向您展示不同的選項,以查看哪些內容適合您的項目。
EDIT
這裏是我使用的形式的插件時,返回在一個textarea的響應使用的代碼。
這裏的上傳動作:
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);
}
}
我發現窗體插件有趣,我想知道如何我可以在asp.net mvc 2中的html文本區域嵌入ajax響應。任何想法 – 2010-07-20 08:15:47
我看到的確實取決於Flash Player: - / – 2011-11-26 18:01:04
This should help與AJAX文件上傳。對於多個文件上傳,快速谷歌顯示the jquery multifile plugin。
- 1. Silverlight選項卡控件 - 一個選項卡上的多個控件
- 2. HTML CSS選項卡控件 - 同一頁面上的多個選項卡控件
- 3. 在選項卡控制器中選擇了兩個選項卡
- 4. 如何在中繼器/網格控件中實現Jquery選項卡?
- 5. 如何實現一個選項卡控件,其中每個選項卡是通用分頁實體?
- 6. 如何把WPF選項卡控制選項卡放在邊上
- 7. 使用MVVM實現選項卡控制頁面功能區選項卡
- 8. jquery多個jsp選項卡
- 9. 多個文件上傳與jquery限制
- 10. 如何在選項卡控件選項卡中嵌套多個groupbox?
- 11. 複製選項卡控件中的選項卡
- 12. 如何在C#中使用垂直製表符實現選項卡控件?
- 13. jquery選項卡,第一個選項卡上的onload事件
- 14. 實現多文件上傳多
- 15. Jquery選項卡實現 - SharePoint 2010
- 16. jQuery UI選項卡:多組選項卡
- 17. Ajax選項卡擴展器控制選項卡上點擊
- 18. 在jQuery UI選項卡上製作一個「添加選項卡」按鈕
- 19. 在選項卡上的控件
- 20. 在dashcode中實現選項卡欄ctrl
- 21. jQuery選項卡中的多個slickGrid
- 22. 如何在ASP.NET MVC3中使用HttpFileCollectionBase實現多個文件選擇和上傳?
- 23. 在C#中更改選項卡控件中的選項卡#
- 24. 計數多個選項卡tsv文件
- 25. JQuery選項卡,選項卡上選擇觸發事件
- 26. 選項卡控件和現有datagridview
- 27. 從stratch實現多文件上傳
- 28. 在一個頁面上的多個Jquery選項卡部分
- 29. jQuery工具選項卡嵌套控件?
- 30. 在多個選項卡上運行宏
如果你需要實際的示例代碼,你必須提供一些關於你的設置的更多細節,或者發佈一些代碼。 – elo80ka 2010-07-19 15:01:47