nsabiyera / Oak

Frictionless development for ASP.NET MVC single page web apps. Prototypical and dynamic capabilities brought to C#.
http://amirrajan.github.com/Oak
MIT License
6 stars 7 forks source link

How to get uploaded file content? #25

Closed davidpodhola closed 11 years ago

davidpodhola commented 11 years ago

I need to upload a file. I add <input type="file" id="scan" name="scan" /> to a view. Now in the Controller I need to read the content. I tried using Request.Files and also tried to dereference @params, but no luck. Please suggest how to get uploaded file content. Thanks!

amirrajan commented 11 years ago

The dynamic model binder was authored to help with the serialization of json data, here is the class for reference:

https://github.com/amirrajan/Oak/blob/master/Oak/ParamsModelBinder.cs

If you want to get access to a file you can use the static model binders that come with mvc (specifically HttpPostedFileBase). I believe this file is still relevent:

http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx

Another option may be to access the file via the HttpContext.Request.Files collection within the controller.

Any ideas how the dynamic model binder can be enhance to include files?

davidpodhola commented 11 years ago

Thanks! Unfortunately HttpContext.Request.Files does not work. Request.Files.Count shows 0. If I dereference @params.scan, I get @params.scan "C:\\Users\\david_000\\Desktop\\SKMBT_C22013021514280-signed.pdf" dynamic {string}. Will try static model binders.

amirrajan commented 11 years ago

Would it be helpful if asking for file returned a byte array? It may be possible to pull that off. Would it be something you'd want to try (we can pair on it some time if you want)?

davidpodhola commented 11 years ago

Yes, that would be perfect, I need byte[] to encode64 it and upload to Dynamics CRM Online. If there is anything to download and test, please let me know, thanks!


Od:Amir Rajan Odesláno:20.4.2013 18:52 Komu:amirrajan/Oak Kopie:David Podhola Pøedmìt:Re: [Oak] How to get uploaded file content? (#25)

Would it be helpful if asking for file returned a byte array? It may be possible to pull that off. Would it be something you'd want to try (we can pair on it some time if you want)?

— Reply to this email directly or view it on GitHubhttps://github.com/amirrajan/Oak/issues/25#issuecomment-16707184.

davidpodhola commented 11 years ago

I am definitely missing something, however simply adding public HttpPostedFileBase scan { get; set; } to my public class ContractForm : DynamicModel do not work. Adding HttpPostedFileBase scan as a second parameter to public dynamic AddCommitPrint(ContractForm @params, HttpPostedFileBase scan) neither.

davidpodhola commented 11 years ago

Forgot to mention - I am doing all this in MVC 4 to be Azure compatible.

amirrajan commented 11 years ago

this worked for me: https://gist.github.com/amirrajan/5460334

Keep in mind that most cloud based servers do not give you access to the file system, so you'll have to find other means to save the file (maybe in the DB or blog storage...I don't have much experience with that).

Let me know if this works for you.

davidpodhola commented 11 years ago

Working, thanks a lot!