ASP.NET文件浏览器获取文件路径
我在 MVC 中使用 作为文件浏览器对话框。但我实际上不想上传该文件,我使用它来选择共享驱动器上的模板。我只想在我的操作方法中获取完整的文件路径。然后服务器将处理该文件并强制下载到客户端。我已经掌握了 HttpPostedFileBase 对象,但没有看到文件路径属性。这可能吗?或者如果没有,还有哪些其他选择?
I'm using an <input type="file" />
as a file browser dialog in MVC. I don't actually want to upload the file though, I am using it to select a template on a shared drive. I only want to get the full filepath in my action method. The server will then process the file and force download to the client. I have got hold of the HttpPostedFileBase
object, but I don't see a filepath property. Is this possible? Or if not, what other options are available?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将永远无法从文件上传框中获取文件的完整路径,因为这是一个安全问题。没有浏览器会向您传递完整路径,而只会传递文件名。
此外,文件上传框将始终上传文件。不幸的是,就像完整路径的安全问题一样,无法显示文件对话框并让用户在不启动上传的情况下选择文件。
You will never be able to get the full path of a file from a file upload box because it is a security issue. No browser will pass you the full path, only the filename.
Additionally, the file upload box will always upload the file. Unfortunately, much like with the security issue of the full path, there is no way to show a file dialog and have the user pick a file without initiating an upload.
您可以尝试在输入上添加 onchange javascript 事件并将值写入隐藏字段之类的操作。然后只需从隐藏字段中读取值即可。它将包含输入字段的文本框部分中的所有内容。
我没有用 MVC 看过它,但在 ASP.Net 中,我可以在使用输入字段后从后面的代码中读取该值。
希望这有帮助。
You could try something like adding an onchange javascript event on the input and write the value to a hidden field. Then just read in the value from the hidden field. It will contain everything that's in the text box part of the input field.
I haven't looked at it with MVC but in ASP.Net I'm able to read that value from the code behind after using the input field.
Hope this helps.