HtmlInputFile and property's

G

Guest

I'm uploading files to a location on my server. It works fine accept, I can't
seem to get the control over the max file size to upload and the types I want
to only allow.
Max file size = 50KB
Types = .gif and .jpg

I've tried using the accept property but it doesn't work, the dialog opens
asking for *.* all files and allows them to be uploaded. And I can't restrict
file size?

Is there a way to set this in the code-behind routine via the HtmlInputFile
properties?

i.e.
<input id="inpFileUp" Type="file" Accept="image/gif" runat="server" />

I'm using the MS routine that works pretty well, found on their article:

Protected WithEvents inpFileUp As HtmlInputFile

If Not inpFileUp.PostedFile Is Nothing And
inpFileUp.PostedFile.ContentLength > 0 Then

Dim fn As String =
System.IO.Path.GetFileName(inpFileUp.PostedFile.FileName)
Dim SaveLocation As String
SaveLocation = Server.MapPath("\xxxx\xxxxxxx\" & fn)
Try
inpFileUp.PostedFile.SaveAs(SaveLocation)
Response.Write("The file has been uploaded.")
Catch Exc As Exception
Response.Write("Error: " & Exc.Message)
End Try
Else
Response.Write("Please select a file to upload.")
End If

Thanx a bunch.
 
K

Kevin Spencer

Hi Chris,

An HtmlInputFile renders an "input type='file'" HTML element on the client.
There is no way to know what the file size is until it is uploaded. The only
data in the HTML element is the path to the file on the client machine. All
you can do is either accept or reject the file after uploading. You CAN
restrict file types by checking the file extension in the "value" property
of the input object on the client, since the full path including the file
extension is in the box. Other than that...

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
B

bruce barker

IE hasn't implemented the Accept feature. you control the max upload size in
the web config (maxRequestLength). note: if the user tries to upload a
larger file, you can not detect and give an error, the upload is stopped by
closing the connection. the browser generally reports a page not found
error.

-- bruce (sqlwork.com)



| I'm uploading files to a location on my server. It works fine accept, I
can't
| seem to get the control over the max file size to upload and the types I
want
| to only allow.
| Max file size = 50KB
| Types = .gif and .jpg
|
| I've tried using the accept property but it doesn't work, the dialog opens
| asking for *.* all files and allows them to be uploaded. And I can't
restrict
| file size?
|
| Is there a way to set this in the code-behind routine via the
HtmlInputFile
| properties?
|
| i.e.
| <input id="inpFileUp" Type="file" Accept="image/gif" runat="server" />
|
| I'm using the MS routine that works pretty well, found on their article:
|
| Protected WithEvents inpFileUp As HtmlInputFile
|
| If Not inpFileUp.PostedFile Is Nothing And
| inpFileUp.PostedFile.ContentLength > 0 Then
|
| Dim fn As String =
| System.IO.Path.GetFileName(inpFileUp.PostedFile.FileName)
| Dim SaveLocation As String
| SaveLocation = Server.MapPath("\xxxx\xxxxxxx\" & fn)
| Try
| inpFileUp.PostedFile.SaveAs(SaveLocation)
| Response.Write("The file has been uploaded.")
| Catch Exc As Exception
| Response.Write("Error: " & Exc.Message)
| End Try
| Else
| Response.Write("Please select a file to upload.")
| End If
|
| Thanx a bunch.
|
|
 
G

Guest

how do you check/limit the extension then?
It states using Accept="image/gif" but that doesn't seem to work. I was sure
that the dialog box limits the types it will allow "browsing" for in addition.
 
K

Kevin Spencer

how do you check/limit the extension then?

Using JavaScript, check the string in the "value" property of the input.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top