2012-01-10 19 views
2

控件我有一個上傳管理,我想返回一個文件時,用戶名爲My控制的這樣一個特殊的方法的方法:返回文件在MVC3

www.website.com/upload/getfile/?fileID=100 

我怎麼能做到這一點? 我想知道如何返回文件!


我發現我的答案,我寫了下面的示例代碼:

public FilePathResult GetFile(string Name) 
    { 
     FilePathResult s = new FilePathResult(@"C:/"+Name, "File"); 
     Response.Headers.Clear(); 

     return s; 
    } 

,但有一個問題now.Is那裏,如果我用File我ContentType.Because我不要任何問題不瞭解它。

回答

2

將名爲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(); 
    } 
} 
+0

更改的ViewResult到FileResult。 – 2012-01-10 04:38:52

+0

編輯問題! – 2012-01-10 04:39:00

+0

脫穎而出詳情http://stackoverflow.com/questions/1187261/whats-the-difference-between-the-four-file-results-in-asp-net-mvc – 2012-01-10 04:41:11

1

你可以試試這個

public void getFile(fileId id) 
{ 
    FileInfo fileInfo = GetFileInfo(id); //Your function, which returns File Info for the Id 
    Response.ClearContent(); 
    Response.ClearHeaders(); 
    Response.AddHeader("Content-Length", fileInfo.Length.ToString()); 
    Response.TransmitFile(fileInfo.FullName); 
    Response.ContentType = "CONTENT TYPE"; 
    Response.Flush(); 
} 

我是用這個來從服務器的MP3文件。

希望這會有所幫助。