We help IT Professionals succeed at work.

Using multiple in select creates spaces??

Bob Schneider
on
I am trying to attach files to an email by allowing a user to select file(s) from a select ojbect and it keeps adding a space between the file path and any files attached after the first.  Confusing?  Here is what I am using to get the file:

<select name="file_links" id="file_links" multiple>
	<%For i = 0 to UBound(FileArr) - 1%>
		<option value="<%=FileArr(i)%>"><%=FileArr(i)%></option>
	<%Next%>
</select>

Open in new window


Here is how I am breaking the string up:
	Dim sFiles
	Dim Files()
	
	sFiles = Request.Form.Item("file_links")

	j = 0
	ReDim Files(0)
	For i = 1 To Len(sFiles)
		If Mid(sFiles, i, 1) = "," Then
			Files(j) = CStr(Files(j))
			j = j + 1
			ReDim Preserve Files(j)
		Else
			Files(j) = Files(j) & Mid(sFiles, i, 1)
		End If
	Next

Open in new window


Here is my attachment code:
	If UBound(Files) > 0 Then
		For i = 0 To UBound(Files)
			.AddAttachment "c:\inetpub\h51web\etraxc\file_fldr\files\" & Files(i)
		Next
	End If

Open in new window


And here is the problem:
c:\inetpub\h51web\etraxc\file_fldr\files\CustomBlackBeltsOrderForm.xls
c:\inetpub\h51web\etraxc\file_fldr\files\ rprt_card_code.doc
c:\inetpub\h51web\etraxc\file_fldr\files\ Tortellini Soup.doc

(note the spaces between the file path and the file name in all but the first file..
Comment
Watch Question

Top Expert 2011
Commented:
Please try this code:

Dim sFiles
	Dim Files()
	
	sFiles = Request.Form.Item("file_links")

	j = 0
	ReDim Files(0)
	For i = 1 To Len(sFiles)
		If Mid(sFiles, i, 1) = "," Then
			Files(j) = CStr(Trim(Files(j)))
			j = j + 1
			ReDim Preserve Files(j)
		Else
			Files(j) = Files(j) & Mid(Trim(sFiles), i, 1)
		End If
	Next

Open in new window

What happens if you have a simple:

Files = Split(sFiles,",")
Big MontyWeb Ninja at large
CERTIFIED EXPERT
Commented:
may I suggest another way to put the list of files into an array? I would use the Split() function. it'll take a string and split it into an array based upon the specified delimeter:

Dim sFiles
Dim Files()
	
sFiles = Request.Form.Item("file_links")
Files = Split( sFiles, "," )

Open in new window


when passing multiple values from a single select box a comma will be used to seperate the values. my guess is an extra space was being added in your loop, and thats why it was appearing in the file path
Bob SchneiderCo-Owner

Author

Commented:
pateljitu, I tried that.  It takes any spaces out of the file name if there are some and then doesn't locate the file.

I will try the Split approach and see what happens...
Bob SchneiderCo-Owner

Author

Commented:
Got a type mismatch on the last line:


      Dim sFiles
      Dim Files()
      
      sFiles = Request.Form.Item("file_links")
      Files = Split(sFiles, "," )
Big MontyWeb Ninja at large
CERTIFIED EXPERT
Commented:
Try removing

Dim Files()
Top Expert 2011
Commented:
Also in case you only want to remove trailing spaces from left of string, please try LTRIM() instead of TRIM() function.

For e.g. Files(j) = CStr(LTrim(Files(j)))
Bob SchneiderCo-Owner

Author

Commented:
I removed Dim FIles() and it threw an error so I re-added Dim Files and the email sent but without an attachment.  I tried the LTrim thing earlier and to no avail.  This really has me puzzled...
Big MontyWeb Ninja at large
CERTIFIED EXPERT
Commented:
can you post the code you're now using?
Bob SchneiderCo-Owner

Author

Commented:
Dim sFiles
      Dim Files
            
      sFiles = Request.Form.Item("file_links")
      Files = Split(sFiles, "," )

Note that I tried it without the "Dim Files" and with "Dim Files()".  The first did not attach anything (I think) and the second generated a type mismatch error.
Web Ninja at large
CERTIFIED EXPERT
Commented:
try putting this right after the code segment above:

for each fileName in Files
   Response.Write "File: " & fileName & "<br/>"
next

does anything get written out to the screen?
Bob SchneiderCo-Owner

Author

Commented:
Got it figured out...added a space after the comma in Split(sFiles, ",") so that it is now Split(sFiles, ", ")

Thanks all for the help
Bob SchneiderCo-Owner

Author

Commented:
Great assistance!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.