將名爲uploadController
控制器,與名爲getfile
其中有一個參數的作用。
接着上面的URL可以改變
www.website.com/upload/getfile/100
UPDATE:
更改爲FileResult
對於完整的答案返回類型的操作來看看我的代碼庫的一部分:
//Attachment Class
public class Attachment
{
#region Properties
public virtual Guid AttachmentId { get; set; }
public virtual int? ContentLength { get; set; }
public virtual string ContentType { get; set; }
public virtual byte[] Contents { get; set; }
public virtual DateTime? DateAdded { get; set; }
public virtual string FileName { get; set; }
public virtual string Title { get; set; }
#endregion
}
public class AttachmentController : Controller
{
IAttachmentService attachmentService;
public AttachmentController(IAttachmentService attachmentService)
{
this.attachmentService = attachmentService;
}
public ActionResult Index(Guid id)
{
var attachment = this.attachmentService.GetById(id);
return attachment.IsNull() ? null : this.File(attachment.Contents, attachment.ContentType,attachment.FileName);
}
}
public class AttachmentModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
HttpRequestBase httpRequestBase = controllerContext.RequestContext.HttpContext.Request;
HttpPostedFileBase @base = httpRequestBase.Files[bindingContext.ModelName];
var converter = new FileConverter();
Attachment attachment = converter.Convert(
new ResolutionContext(
new TypeMap(new TypeInfo(typeof(HttpPostedFileWrapper)), new TypeInfo(typeof(Attachment))),
@base,
typeof(HttpPostedFileWrapper),
typeof(Attachment)));
}
return attachment;
}
}
public class MvcApplication : HttpApplication
{
protected void Application_Start()
{
ModelBinders.Binders[typeof(Attachment)] = new AttachmentModelBinder();
}
}
更改的ViewResult到FileResult。 – 2012-01-10 04:38:52
編輯問題! – 2012-01-10 04:39:00
脫穎而出詳情http://stackoverflow.com/questions/1187261/whats-the-difference-between-the-four-file-results-in-asp-net-mvc – 2012-01-10 04:41:11