我试图在上传后将每个单独文件的名称保存在数据库中。我有成功上传文件的代码。
$(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;
}
最终,我只是想遍历名称并将它们保存在数据库中。任何帮助表示赞赏。谢谢