ASP .NET MVC 2 - 浏览并添加文档链接

发布于 2024-12-27 16:04:17 字数 1190 浏览 1 评论 0原文

我正在使用 MVC 2 开发一个内部项目管理仪表板。要求之一是链接到我们本地服务器之一上现有的文档,换句话说,浏览到服务器,选择文件,单击“添加”,该项目的视图将包含链接。这是我所拥有的(为了简洁,我省略了一些细节):

模型:

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

视图:

<%
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" />
<%} %>

控制器:

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

所以,正如你所看到的,我正在使用 html 文本框进行文件上传,但实际上并没有上传,只是尝试获取路径和文件名并将其用作链接。然而,由于安全限制,这仅适用于 IE,因为没有其他浏览器可以让您获取该路径。此外,如果用户使用映射驱动器,它将无法工作,因为不会使用完整路径,因此他们必须直接导航到服务器。

有人能想出另一种方法来做到这一点吗?我希望能够使用上传功能提供的浏览功能,但不受约束。

目前,我能想到的唯一(低技术)解决方案是用户将链接显式粘贴到文本框中。但更喜欢更友好的东西。

提前致谢。也是第一次发布问题,所以请友善:-)

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 :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丑丑阿 2025-01-03 16:04:17

如果我是您,我会允许他们上传新文件或粘贴到现有文件的位置。没有理由尝试重用文件上传元素来完成您正在做的事情。

表单示例(不想写出<%=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>

模型

public enum AddDocumentType
{
    New,
    Link
}

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

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; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文