从文件 HTML 输入的代码隐藏中获取完整文件路径

发布于 2024-10-03 10:50:50 字数 489 浏览 5 评论 0 原文

我使用文件 HTML 输入而不是文件上传 Web 控件。别问为什么,但我必须这么做!

HTML:

<input type="file" id="image1" class="listUploadAdd" name="ImageAdd1" />

代码隐藏:

Dim ImageAdd1 As String = Request.Form("ImageAdd1").ToString()

我从“C:/Orange.jpg”浏览上传,代码隐藏中的结果只是图像名称“Orange.jpg”,而不是从本地驱动器获取要上传的文件所需的完整“C:/Orange.jpg”。

从代码后面捕获完整图像路径并将其上传到服务器的最佳方法是什么?

谢谢。

I used file HTML input instead of FileUpload Web Control. Don't ask why but I just have to!

HTML:

<input type="file" id="image1" class="listUploadAdd" name="ImageAdd1" />

Code behind:

Dim ImageAdd1 As String = Request.Form("ImageAdd1").ToString()

I browsed from "C:/Orange.jpg" to upload and the result in Code Behind is just the image name "Orange.jpg" and not the full "C:/Orange.jpg" which is needed to get the file from local drive to be uploaded.

What is the best way to capture the full image path from code behind to be uploaded onto the server?

Thank you.

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

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

发布评论

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

评论(2

梦太阳 2024-10-10 10:50:50

您可以将 runat="server" 添加到输入元素吗? :) 那么将文件存储在服务器上就非常容易了

Request.Files("File1").SaveAs("c:\somedir\yourfile.txt")

。如果您想要原始文件名和路径,请尝试以下操作:

Dim filename As string = Request.Files("File1").FileName

要在不使用 runat="server" 属性的情况下下载文件,您可以执行以下操作:

Dim file = Request.Files("File1")
Dim buf(file.ContentLength) As Byte
file.InputStream.Read(buf, 0, file.ContentLength)
IO.File.WriteAllBytes("C:\somedir\yourfile.txt", buf)

但是你必须在html页面的form元素上设置enctype

<form id="yourform" runat="server" enctype="multipart/form-data">

Can you add a runat="server" to the input element? :) Then it's pretty easy to store the file on your server

Request.Files("File1").SaveAs("c:\somedir\yourfile.txt")

If you want the original filename and path, try this:

Dim filename As string = Request.Files("File1").FileName

To download the file without using a runat="server" attribute, you can do this:

Dim file = Request.Files("File1")
Dim buf(file.ContentLength) As Byte
file.InputStream.Read(buf, 0, file.ContentLength)
IO.File.WriteAllBytes("C:\somedir\yourfile.txt", buf)

But you have to set the enctype on the form element in the html page:

<form id="yourform" runat="server" enctype="multipart/form-data">
淡淡绿茶香 2024-10-10 10:50:50

我从“C:/Orange.jpg”浏览并上传,代码隐藏中的结果只是图像名称“Orange.jpg”,而不是获取图像所需的完整“C:/Orange.jpg”从本地驱动器上传文件。

您将无法从客户端计算机上提取文件。当您开发程序时,客户端和服务器是同一台机器,但当您部署它时,服务器和客户端将是不同的机器。当后面的代码执行时(在服务器上),如果您尝试打开 C:\Orange.jpg,您将尝试从服务器的硬盘上打开它。该文件可能不存在。

当您从网页上传文件时,该文件将作为 POST 消息的一部分发布到服务器。您可以从 Form 集合中获取该文件。您无需将上传器转换为 ASP.NET 控件,也无需添加 runat="server" 属性。只要您发布包含输入元素的表单,它将被提交到服务器。

文件的内容将作为字节数组存储在表单中。您可以将此字节数组作为文件保存到服务器硬盘的某个位置。

总之:

您不需要知道客户端计算机上文件的路径,因为您无论如何都无法访问它。使用作为表单提交的一部分发布的文件数据来在服务器上保存文件的副本。

I browsed from "C:/Orange.jpg" to upload and the result in Code Behind is just the image name "Orange.jpg" and not the full "C:/Orange.jpg" which is needed to get the file from local drive to be uploaded.

You won't be able to pull the file off the client's machine. When you're developing your program, the client and the server are the same machine but when you deploy it, the server and client will be different machines. When the code behind executes (on the server), if you try to open C:\Orange.jpg you will be trying to open it off the server's hard disk. This file will probably not exist.

When you upload a file from a web page, it will be posted to the server as part of the POST message. You can get the file out of this from the Form collection. You don't need to convert the uploader to the ASP.NET control, or add the runat="server" attribute. So long as your post the form, which contains the input element it will be submitted to the server.

The file's contents will be stored as a byte array in the form. You can save this byte array as a file to the server's hard disk somewhere.

In summary:

You don't need to know the path to the file on the client's machine, since you can't access it anyway. Use the file data that is posted as part of the form submit instead to save a copy of the file on the server.

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