Link to home
Start Free TrialLog in
Avatar of code1994
code1994

asked on

image upload using vb.net

hello:

im trying to upload the images to server i have code for that and its work fine
but i have 3 input box and i have to write the same code 3 times but instead of writing 3 times same code except the change of input id name. my question that, how can i convert my code into for - next loop so that regardles how many input i have, i just need to pass how many i have in my form

<input id="fileupload1" style="Z-INDEX: 116; LEFT: 147px; POSITION: absolute; TOP: 149px"
type="file" name="fileupload1" runat="server">

<input id="fileupload1" style="Z-INDEX: 116; LEFT: 147px; POSITION: absolute; TOP: 149px"
type="file" name="fileupload1" runat="server">

<input id="fileupload1" style="Z-INDEX: 116; LEFT: 147px; POSITION: absolute; TOP: 149px"
type="file" name="fileupload1" runat="server">


If Len(fileupload1.Value) > 0 Then
            If Not (fileupload1.PostedFile Is Nothing) Then 'Check to make sure we actually have a file to upload
                'Dim strLongFilePath As String =
                strLongFilePath = fileupload1.PostedFile.FileName()
                'Dim intFileNameLength As Integer =
                intFileNameLength = InStr(1, StrReverse(strLongFilePath), "\")
                'Dim strFileName As String =
                strFileName = Mid(strLongFilePath, (Len(strLongFilePath) - intFileNameLength) + 2)

                Select Case fileupload1.PostedFile.ContentType
                    Case "image/pjpeg", "image/jpeg", "image/gif", "image/bmp"    'Make sure we are getting a valid JPG image
                        'fileupload1.PostedFile.SaveAs(Server.MapPath("\uploadimages\") & strFileName)
                        fileupload1.PostedFile.SaveAs(ConfigurationSettings.AppSettings("FilePath") & "\" & strFileName)
                        lbStatus.Text = strFileName & " was uploaded successfully to: " & Server.MapPath("\") & strFileName
                    Case Else
                        'Not a valid jpeg image
                        lbStatus.Text = "Not a valid jpeg image"
                End Select
            End If
        End If
Avatar of code1994
code1994

ASKER

sorry

fileupload1
fileupload2
fileupload3

<input id="fileupload1" style="Z-INDEX: 116; LEFT: 147px; POSITION: absolute; TOP: 149px"
type="file" name="fileupload1" runat="server">

<input id="fileupload2" style="Z-INDEX: 116; LEFT: 147px; POSITION: absolute; TOP: 149px"
type="file" name="fileupload1" runat="server">

<input id="fileupload3" style="Z-INDEX: 116; LEFT: 147px; POSITION: absolute; TOP: 149px"
type="file" name="fileupload1" runat="server">
Hi code1994,
You can use HttpFileCollection like :
System.Web.HttpFileCollection _files = System.Web.HttpContext.Current.Request.Files;
to loop through the files upload.
Pls check here for more information
http://www.codetoad.com/asp.net_multiplefileupload.asp 
Nice day

hi, the code is return in aspx page not on aspx.vb

can i put the code into .vb ?
Yes, ofcourse
Put it in Click Event of your upload button
Sub UploadMultipleFiles_Clicked(ByVal Sender As Object, ByVal e As EventArgs) handles your_upload_button.OnClick
i able to put the code on .vb but  i run one small problem

i want to check what type of file they are uploading right now im restricting to jpg/gif/bmp can you shed some light on this please?

thank you
i want to do the validation on aspx page like in javascript  of aspx

if the user upload other then the jpg/gif/bmp and hit submit should give the alert that they can not upload

thanks
Hi, you can use like this :
Select Case strFile.PostedFile.ContentType
                    Case "image/pjpeg"
                        Upload()
                    Case "image/jpg"
                        Upload()
                    Case "image/gif"
                        Upload()
                    Case Else
                                                response.write(strFile.PostedFile.ContentType + " : Not a valid Picture")
                End Select
i want on the client side validation like javascript please
i have some code but its not working propery:

pic = is the name of my texbox

but i want to make this more like a general fucntion anybody can use

i have like 3 input

function check() {
alert("imhere");
  var ext = document.all.pic.value;
  ext = ext.substring(ext.length-3,ext.length);
  ext = ext.toLowerCase();
  if(ext != 'jpg') {
    alert('You selected a .'+ext+' file; please select a .jpg file instead!');
    return false; }
  else
    return true;  
}
sorry, I mixed up. Hope anyone can help you with Javascript, I don't dig much in it.
ASKER CERTIFIED SOLUTION
Avatar of gbelken99
gbelken99

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial