2

I Have an Upload Manager and I want to return a File when user called a special Method of My Control Like this:

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

how Can I do this? I want to know how Can I return a file!


I found my answer and I wrote the sample code below:

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

        return s;
    }

but there is a problem now.Is there any problem if I use File for my ContentType.Because I don't know it.

ahmadali shafiee
  • 4,350
  • 12
  • 56
  • 91

3 Answers3

2

Make a controller named uploadController, with an action named getfile which has an argument.

Then the above url can be changed to

www.website.com/upload/getfile/100

UPDATE:

Change the return type of action to FileResult

For complete answer take a look at part of my codebase:

//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();
    }
}
Jahan Zinedine
  • 14,616
  • 5
  • 46
  • 70
1

Sounds like you want to return a FileResult http://msdn.microsoft.com/en-us/library/system.web.mvc.fileresult.aspx

Dale K
  • 25,246
  • 15
  • 42
  • 71
1

Can you try this

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();
}

I was using this to get MP3 file from server.

Hope this helps.

Amar Palsapure
  • 9,590
  • 1
  • 27
  • 46