2
我想保存每個單獨的文件的名稱在數據庫中,他們已被上傳後。我有這個成功上傳文件的代碼。保存文件名後上傳,使用plupload和MVC3
$(function() {
$("#uploader").plupload({
// General settings
runtimes: 'silverlight,flash,html5',
url: '@Url.Content("~/FileUploadChunks/UploadChunk")',
max_file_size: '10mb',
chunk_size: '1mb',
unique_names: true,
// Flash settings
flash_swf_url: '/plupload/js/plupload.flash.swf',
// Silverlight settings
silverlight_xap_url: '/plupload/js/plupload.silverlight.xap'
});
});
這是控制器
[HttpPost]
public ActionResult UploadChunk(int? chunk, int chunks, string name)
{
var fileUpload = Request.Files[0];
var uploadpath = Server.MapPath("~/App_Data/UploadedFiles");
chunk = chunk ?? 0;
using (var fs = new FileStream(Path.Combine(uploadpath, name), chunk == 0 ? FileMode.Create : FileMode.Append))
{
var buffer = new byte[fileUpload.InputStream.Length];
fileUpload.InputStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, buffer.Length);
}
return Content("chunk uploaded", "text/plain");
}
只是爲了測試,我想這樣的事情,儘量爭取名稱,並將它們添加到列表返回到視圖,但我想不出如何在視圖中打印列表以查看其內容。
[HttpPost]
public ActionResult UploadChunk(int? chunk, int chunks, string name)
{
var fileUpload = Request.Files[0];
var uploadpath = Server.MapPath("~/App_Data/UploadedFiles");
chunk = chunk ?? 0;
using (var fs = new FileStream(Path.Combine(uploadpath, name), chunk == 0 ? FileMode.Create : FileMode.Append))
{
var buffer = new byte[fileUpload.InputStream.Length];
fileUpload.InputStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, buffer.Length);
}
List<string> list = new List<string>();
foreach (string inputTagName in Request.Files)
{
HttpPostedFileBase file1 = Request.Files[inputTagName];
if (file1.ContentLength > 0)
{
list.Add(file1.FileName);
}
}
ViewBag.List = list;
}
最終我只是想通過名稱循環並將它們保存在數據庫中。任何幫助表示讚賞。由於
感謝您的幫助,我仍然試圖得到它的工作。我正在使用鏈接到SQL。究其原因,@foreach視圖不會工作是最初是空的,並沒有得到任何內容,所以它拋出一個錯誤。我需要以某種方式刷新它提交後新加載的數據。謝謝 – user973671