1

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.

2 Answers 2

2

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">
Sign up to request clarification or add additional context in comments.

4 Comments

Not sure what I'm not doing right. I got this error "Object reference not set to an instance of an object". Have the same message for "Dim filename As String = Request.Files("image1").FileName" OR "Dim filename As String = Request.Files("ImageAdd1").FileName". I put the same Form attributes as you mentioned too. Strange really.
But as @Michael Shimmins pointed out, you really shouldn't be concerned about where the file was located on the client machine. What's important, is where you chose to store it on your server.
You're probably getting the null reference exception when trying to access file. Request.Files("File1") is probably returning null.
Got it working. Turns out I shouldn't use "runat=server" for <input type="file" ... >. Thank you.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.