Snuffler_71
Snuffler_71

Reputation: 386

ASP .NET MVC 2 - Browse and add link to document

I am developing an internal project managment dashboard using MVC 2. One of the requirements is to link to already existing documents on one of our local servers, in other words, browse to server, select file, click add and the view for that project will contain the link. Here is what I have (I've left some details out for brevity):

Model:

public class AddDocumentModel
{
    public HttpPostedFileBase DocumentLink { get; set; }
}

View:

<%
using (Html.BeginForm(MVC.ProjectDetails.Actions.AddDoc(this.Model.ProjectID), 
                FormMethod.Post, new { enctype = "multipart/form-data" }))
{%> 

<%=Html.TextBoxFor(a => a.DocumentLink, 
                    new { type = "file", style = "width:100%;"})%>

   <input type="submit" value="Add Document Link" />
<%} %>

Controller:

[AcceptVerbs(HttpVerbs.Post)]
public virtual ActionResult AddDoc(AddDocumentModel docModel)
{
    var model = _projectManagementService.AddDocumentLink(
                        docModel.DocumentLink.FileName);
}

So, as you can see I am using an html textbox for file upload but not actually uploading, just attempting to grab path and filename and use that as link. However due to security constraints this will only work in IE, as no other browser will let you get at the path. Also, if the user uses a mapped drive it won't work as the full path will not be used, so they have to navaigate directly to the server.

Can anyone think of another way to do this? I would like to be able to use the browse functionality offered by upload functionality, but not be tied by constraints.

At the moment the only (low tech) solution I can think of is for the user to explicitly paste the link into a text box. But would prefer something a lot more friendly.

Thanks in advance. First posted question too, so be kind :-)

Upvotes: 1

Views: 386

Answers (1)

hunter
hunter

Reputation: 63562

If I were you I would allow them to either upload a new file or paste in the location of an existing file. There's no reason to try to reuse a file upload element to do what you're doing.

Form example (didn't feel like writing out the <%=Html %>

<form>
    <div>
        <input type="radio" name="AddDocumentType" value="New" />
        <label for="NewDocument">Upload New Document</label>
        <input type="file" id="NewDocument" name="NewDocument" />
    </div>
    <div>
        <input type="radio" name="AddDocumentType" value="Link" />
        <label for="LinkDocument">Link To Existing Document</label>
        <input type="text" id="LinkDocument" name="LinkDocument" />
    </div>
    <input type="submit" value="Add Document Link" />
</form>

Model

public enum AddDocumentType
{
    New,
    Link
}

public class AddDocumentModel
{
    public AddDocumentType AddDocumentType { get; set; }
    public HttpPostedFileBase NewDocument { get; set; }
    public string LinkDocument { get; set; }
}

Upvotes: 2

Related Questions