In our ASP MVC3, we need to allow the user to navigate to a shared folder on our LAN and select the file they want associated with a particular item. We want to maintain one copy of the item, so we don't want to do any uploading/downloading, we just want to store the specified file path as a field in a SQL table. What is the best method to do this? Right now I can use this helper open a file browser window and select the file, however only the file name gets stored.
#Html.TextBoxFor(model => model.Attachments[0].Filepath, new { type = "file" })
What is the best method to do this?
You could use a normal input field, not a file field:
#Html.TextBoxFor(model => model.Attachments[0].Filepath)
Now the user can copy paste the file path in this field. That's what HTML has to offer you. If it doesn't suit your needs you always have the possibilities of using some client side scripting such as a Flash movie or Silverlight that will be installed on your clients browsers and might require elevated privileges in order to access the file system.
Related
When my program displays a SaveDialog to save a file I can use the Filter and Filter Index properties to restrict the files displayed to certain extensions. Also, when I set the DefaultExt property then, as the user types a filename, matching files in the folder are displayed as suggestions in a dropdown box and one can be selected with the mouse.
For example if the filter is set to "*.xml" then only filenames matching that extension appear in the the dialog list of files. But if the user type "Test", then you will get a dropdown list of suggestion files like:
TestA.doc
TestB.xml
Test123.pdf
TestX.xml
(if those files are present in the folder)
But I would like the suggestion list to only contain the files that match the filter, such as *.xml. Is that possible? The problem is that users can hit the wrong suggestion and save their file with the wrong extension.
TSaveDialog internally uses the IFileDialog interface of Windows. That interface doesn't offer any way in which to change how to filter files in suggestion dropdown text box of File Name field.
You can :
Implement own dialog from scratch
Use component like DexExpress which is not free
Check result of save dialog after execution of it and validate the user selection file and show proper message to user if the file is not valid
I'm just getting my head wrapped around MVC in .net using VS 2013.
I need a little direction in regards to uploading a file (which is easy) but also inserting data about that image into a database. Specifically I want to allow the end user to enter a description, title etc. about the file being uploaded. On the back-end I want to also add to the meta data a 'Date Created', 'Path to the file', 'Category', and the File Name and a couple other pieces of data that will help with presenting files in the views. I don't want to insert the files in the DB but just use the path in the generated HTML to point to the physical file so the end user can view or download it.
Multiple file types are being used, Audio, Video, Documents, Images.
I can get the file upload to work but writing the controller to accept a file, and end user input, then also add the other fields I need into the database that the user never sees is where I'm stuck. Blending the file upload with the user fields and beack end data is confusing me on how to get all the pieces to work together.
So in short getting File Upload + User Input + non-User Input values all in the same View, Controller, and Model is what I need direction on.
You have to upload your image plus data in a multi-part form.
In your view you will create a POST form that uses "multipart/form-data" encoding, you can then include inputs for model data and your file upload control within the body of the form. When it submits will will create a multi-part form, one part will contain the binary file and another part will contain your data.
On the controller action side you will receive the data with an action akin to
public ActionResult PostFile(MyModel model, HttpPostedFileBase file) {...}
There are numerous posts on SO with more details so I won't go into that.
I am using an #Html.TextBoxFor to load and, potentially, set an attachments file path on an edit page. I can get the file browser window to open and reset the value. However, I cannot get the current, or default, value to load with the page. Here is the syntax I'm using right now:
#Html.TextBoxFor(model => item.Filepath, new { #value = item.Filepath, #type = "file" })
If I'm to understand the question correctly, it appears that you are trying to set the default value of the file path to open. The short answer is that you can't do this. The reason being that if you were able to set the file path in code, you could set the path maliciously to something you wanted to steal from the users machine, and dynamically submit the post via client side code. The user must explicitly say where he wants to browse to. This is a security feature.
So the problem I am trying to solve requires that if the user types in a text box their user name and press enter then the files in the directory will display in the List Box. I need it to only work on the C:\Users directory. I am new to Visual Basic but not to programming so any basic advice would be helpful.
You need to do something like this:
Dim di As New IO.DirectoryInfo("c:\")
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
'list the names of all files in the specified directory
For Each dra In diar1
ListBox1.Items.Add(dra)
Next
You can read more from this site, http://www.developerfusion.com/code/3681/list-files-in-a-directory/
I seek some guidedence here ... ( I'm not sure if this is the best title )
At the moment I prepend a "server name" to the url like this:
server10.example.com
This works fine, except that I need to handle all the subdomains on the IIS and I'm not sure google are happy about jumping around from sub to sub to sub, when it seems the links to the other servers.
I'm kind a hoping for a nice way to archive this wioth asp.net mvc.
Most pages are related to a "server" ... there are however a few info pages, contact, home that dont really need a valid "server" name ... but could just be "na" for not available, but the name need to be maintained, if there is already a selected server, when a user are keeps browsing the site. This needs to be as transparent as possible when I need to create the links to the diffenrent pages.
I could extend the Html Action() extensien to automatically add the selected "server" from the previusly request to the page.
In the format:
/{serverParameter}/{controller}/{action}/{parameterInfo}
And if no server is selected, just add "na" as the {server} placeholder.
I'm not sure if more information is needed, but please let me know if ...
I tired of extracting the selected server from the domain part and the other way also seems better, I just can't think of a good way to structure this ...
Updated
90% of all the pages are about a server that the user select at some point. Could be server10, server9, server20 ... just a name. I want to maintain that information across all pages, after the users has selected it or else I just want it to be f.ex: "empty".
I mostly looking for an easy way of doing this or an alternative ... atm I'm prepending the serverParamter to the url so it ends up being: "serverParameter.example.com".
I want to end up with something like
http://example.com/{server}/{controller}/{action}
instread of
http://{server}.example.com/{controller}/{action}
If I understand your question correctly, you just wish to group different collections of content together above the controller/action level. If that's the case, have you considered using ASP.NET MVC areas?
Just right-click on your project, and choose Add -> Area.... Give it a name (what you're calling "server"), and then you can add content, your own controllers, actions, etc. Under this area. You will automatically be able to access it via /AreaName/Controller/Action/etc.
I went with the already impemented routing in ASP.NET MVC.
{server}/{controller}/{action}
When creating the links it takes the set value for {server} and places the value when generating URL's, so I only need to supply controller and action in the #Html.Action helper method ... this could not have been more easy.
I'm not sure why I did not think about this. One just gotta love routing.