I have been wondering about binding upload file using model in ASP.NET MVC2 or MVC3
Usual implementation that I have seen in controller is using HttpPostedFileBase, that exposes the file(s) from request to the user where implementation can be as follows
public ActionResult AcceptFile(HttpPostedFileBase submittedFile)
{
var inputStream = submittedFile.InputStream;
var contentLenght = submittedFile.ContentLength;
var fileName = submittedFile.FileName;
var contentType = submittedFile.ContentType;
return View();
}
The alternative is using classic upload example where possible implementation can be:
public ActionResult UploadFile(HttpPostedFileBase uploadFile)
{
if (uploadFile.ContentLength > 0)
{
string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"),
Path.GetFileName(uploadFile.FileName));
uploadFile.SaveAs(filePath);
}
return View();
}
So after little research I came out with following implementation as I wanted to have one implementation which i can use in more places, without the code repetition.
I can add custom model binders into Global.asax
ModelBinders.Binders.Add(typeof(MyCustomBinderModel), new MyCustomBinderModel());
after this will be looking like:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
ModelBinders.Binders.Add(typeof(MyCustomBinderModel), new MyCustomBinderModel());
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
and finally the implementation in Controller is
public ActionResult UploadFile(MyCustomBinderModel uploadModel){
//do your upload stuff with uploadModel
}
Saturday, 31 March 2012
Tuesday, 27 March 2012
Articles that are opening knowlede
Great article from Scott Hanselman about unit testing file upload using MOCK
A Back To Basics Case Study: Implementing HTTP File Upload with ASP.NET MVC including Tests and Mocks
A Back To Basics Case Study: Implementing HTTP File Upload with ASP.NET MVC including Tests and Mocks
Friday, 23 March 2012
Unexpected error while launching Selenium SE
Unexpected error while launching Selenium SE
I have come across a error while setting up selenium tests on server using Internet Explorer v8.
Unexpected error
launching Internet Explorer. Protected Mode must be set to the same value
(enabled or disabled) for all zones. (NoSuchDriver)
This caused me to wonder, as I know that i have the driver otherwise it would not work on my local PC.
After research i have found the solution. The privacy settings has to be updated.
Solution:
Go to your internet explorer => Tools (IE9 click alt to display menu) => Internet Options
Select Tab Security and un-tick "Enable protected mode" in all tabs. As far I understand the process, this will enable web driver to send commands into the browser.
Internet Explorer |
You might have to ask you administrator to do this for you.
TFS red cross by project / solution
There is an issue
which can arise with Visual Studio 2010 Team Explorer where the TFS project
node will not expand. There is a red cross on the project node to indicate that
it hasn't loaded correctly.
In addition, it is
not possible to publish your web applications.
Both of these issues
are caused by the same root problem.
Visual studio uses
an internet explorer dll to do certain things, and it can happen that this dll
becomes unregistered.
Simply open a normal
command prompt (does not need to be a Visual Studio command prompt) and enter
the following
regsvr32 "C:\Program Files (x86)\Internet
Explorer\ieproxy.dll" or
regsvr32 "C:\Program Files\Internet
Explorer\ieproxy.dll"
Depending on your
version of Windows.
Unlock databases and stop tfs services
CMD TO C:\Program Files\Microsoft Team FoundationServer 2010\Tools on server and run commands below
TFSServiceControlquiesce <<<<<<<<<<<<<< UNLOCK DATABASES, STOPS TFS SERVICES
TFSConfig RemapDBs/DatabaseName:basv-bds-001;TFS_Configuration /SQLInstances:basv-bds-001/Continue
TFSConfig Accounts/ResetOwner /SQLInstance:basv-bds-001 /DatabaseName:Tfs_Configuration
TfsConfig Accounts/add /AccountType:ApplicationTier /account:KCTC\TFSService/SQLInstance:basv-bds-001 /DatabaseName:Tfs_Configuration
TFSConfig RemapDBs/DatabaseName:basv-bds-001;TFS_Configuration /SQLInstances:basv-bds-001/Continue
TFSConfig registerDB/sqlInstance:basv-bds-001 /databaseName:Tfs_Configuration
TFS Mvc does not exists in the namespace
TFS MVC
The type or namespace name 'Mvc' does not exist in the
namespace 'System.Web' (are you missing an assembly reference?)
And solution is:
Install mvc framework into your pc / server where you are running the build.
References
I came to
the conclusion because of sugestion here:
Subscribe to:
Posts (Atom)