I have used the code provided in this link for the upload-handling:
http://towardsnext.wordpress.com/2009/04/17/file-upload-in-aspnet-mvc/
But this isn't working for me. (when I click the "Upload file" button when I'm running the site nothing happens.) I have edited my view like this
<script type="text/javascript">
$(function () {
$("#dialog").dialog({bgiframe: true, height: 140, modal: true, autoOpen: false, resizable: false}) });
</script>
and also
<div id="dialog" title="Upload files">
<% using (Html.BeginForm("Upload", "Upload", FormMethod.Post, new { enctype = "multipart/form-data", id = item.OrderId })){%>
<p><input type="file" id="fileUpload" name="fileUpload" size="23"/> </p>
<p><input type="submit" value="Upload file" /></p>
<% } %>
</div>
<a href="#" onclick="jQuery('#dialog').dialog('open'); return false">Upload开发者_C百科 File</a>
My Upload-function (defined in UploadController) looks like this:
using System;
using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Ajax; using System.IO; using SiteVCM.Models;
namespace SiteVCM.Controllers { public class FileDescription { public string Name { get; set; } public string WebPath { get; set; } public long Size { get; set; } public DateTime DateCreated { get; set; } }
public class UploadController : Controller
{
StoreEntities storeDB = new StoreEntities();
//
// GET: /Upload/
FileRepository fileRepository = new FileRepository();
//public ActionResult Index()
//{
// return View(fileRepository.GetAllFileDescription());
//}
public ActionResult Upload(int id)
{
foreach (string inputTagName in Request.Files)
{
HttpPostedFileBase file = Request.Files[inputTagName];
if (file.ContentLength > 0)
{
string filePath = Path.Combine(HttpContext.Server.MapPath("../Samples")
, Path.GetFileName(file.FileName));
file.SaveAs(filePath);
var order = storeDB.Orders.Single(c => c.OrderId == id);
order.Url = filePath;
storeDB.SaveChanges();
}
}
return RedirectToAction("Index", "Status", new { id = 0 });
}
}
and the FileRepository.cs file is the same except for the namespacing and the place to ut the files in.
The JQuery Dialog box pops up like it should but when I click on the "Upload File" button nothing happens.
The view this code is in belongs to antoher controller then the one my Upload-function is defined in.
Please help me I've been looking for hours to fix this and I freaking me out.
Thanks in advance!!
This works for sure
<% using (Html.BeginForm("Upload", "Upload", FormMethod.Post,
new { enctype = "multipart/form-data" })){%>
<input type="file" id="fileUpload" name="fileUpload"/>
<input type="submit" value="Upload file" />
<% } %>
public ActionResult Upload(HttpPostedFileBase fileUpload)
{
//fileUpload == posted file
}
精彩评论