tags:

views:

76

answers:

7

I have the following code in child window which is working but what I want to do is instead of using response.write I want to use label control or to display all the filename like this:

music.pdf, inventory.doc

My Ultimate goal is to pass the values in string (e.g.: "music.pdf, inventory.pdf" ) to the parent window.

How do I do it?

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
    Try
        '' Get the HttpFileCollection
        Dim hfc As HttpFileCollection = Request.Files
        For i As Integer = 0 To hfc.Count - 1
            Dim hpf As HttpPostedFile = hfc(i)
            If hpf.ContentLength > 0 Then
                hpf.SaveAs(Server.MapPath("/ServerName/DirectoryName") & "\"" & System.IO.Path.GetFileName(hpf.FileName))
                Response.Write("<b>File: </b>" & hpf.FileName & " <b>Size:</b> " & hpf.ContentLength & " <b>Type:</b> " & hpf.ContentType & " Uploaded Successfully! <br/>")
            Else
                Response.Write("Please select a file to upload.")
            End If
        Next i

    Catch ex As Exception

    End Try

End Sub
A: 

You can use one of the String.Join() overloads.

Daniel Brückner
sample code please.
codeLearner
A: 

Use a string builder to join strings. Strings in .NET are immutable, so Join and any other operators are memory intensive

Comment (not on the question, but I noticed it in your code) Use Path.Combine and any other Path functions. Amazingly easy to forget that they exist and have saved me millions of headaches.

Nate Noonen
This will not accomplish the output that the OP wants.
maxwellb
sample code please.
codeLearner
Was this before or after the edit?
Nate Noonen
http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx
Nate Noonen
Does String.Join not use efficient methods before returning a string? I.e., I thought that String.Join uses a string builder during joining, and then returns an immutable string?
maxwellb
@Nate Noonen: My earlier comment was to an earlier version of this answer.
maxwellb
http://www.codeproject.com/KB/cs/StringBuilder_vs_String.aspxI stand corrected: "The shiny performance saving StringBuilder does not help in all cases and is, in some cases, slower than other functions. When you want to have good string concatenation performance I recommend strongly that you use String.Join which does an incredible job." I assumed builder was "always" the best option. You learn something new every day. I love this field :D
Nate Noonen
And the best quote from the article: "After taking a deep look with Reflector I found that String.Join has the most efficient algorithm implemented which allocates in the first pass the final buffer size and then memcopy each string into the just allocated buffer. This is simply unbeatable."
Nate Noonen
+1  A: 

Consider using a <asp:BulletedList>, which is an ASP.NET server control for an unordered list <ul>.

ASPX:

<asp:BulletedList 
   id="uploadedFiles" runat="server"></asp:BulletedList>

Instead of Response.Write() each file, simply:

  Dim displayText as String = string.Format("File: {0}  Size: {1}  Type: {2} Uploaded Successfully", _
          hpf.FileName ,hpf.ContentLength ,hpf.ContentType)

  uploadedFiles.Items.Add(New ListItem(displayText, hpf.FileName ))
p.campbell
+1  A: 

This is not the way I would do it but you asked for your code to work with a Label.

ASPX:

<asp:Label id="lblUploadMsg" runat="server" visible="false"></asp:Label>

Code:

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
    Try
        '' Get the HttpFileCollection
        Dim hfc As HttpFileCollection = Request.Files

        lblUploadMsg.Text = String.empty
        For i As Integer = 0 To hfc.Count - 1
            Dim hpf As HttpPostedFile = hfc(i)
            If hpf.ContentLength > 0 Then
                hpf.SaveAs(Server.MapPath("/ServerName/DirectoryName") & "\"" & System.IO.Path.GetFileName(hpf.FileName))
                lblUploadMsg.Visible = True;                   
                lblUploadMsg.Text +="<b>File: </b>" & hpf.FileName & " <b>Size:</b> " & hpf.ContentLength & " <b>Type:</b> " & hpf.ContentType & " Uploaded Successfully! <br/>"


            Else
                lblUploadMsg.Text="Please select a file to upload."
            End If
        Next i

        ''HTML Encode lblUploadingMsg.Text after

    Catch ex As Exception

    End Try

End Sub
rick schott
Wouldn't the lblUploadMsg.Text = String.empty keep clearing out the label?
Andy Robinson
Bug, I moved it up where it should have been.
rick schott
I was on the right track but didn't utilize the "+=" operator. How dumb of me. Thanks for the solution!!!
codeLearner
As below, it seems that the Label.Text property sends unescaped HTML to the browser. Does `hpf.FileName` have a chance of containing HTML?
maxwellb
A: 

Move your Response.Write outside of the loop over i.

Inside of i loop, keep track using a List<String>. Then, after the loop, use the Response.Write, to write a String.Join over the List.ToArray with a comma.

maxwellb
+1  A: 

Add a label control onto the form/control and then instead of using Response.Write, why not use a string builder and append all of the output to that in the loop. Once you have processed all of the files set the Label control's Text property of the string builder ToString method.

Hope this helps

WestDiscGolf
sample code please
codeLearner
pretty much as per @PhilPursglove 's answer; if further is required then let me know :-)
WestDiscGolf
A: 

Using a Label control won't honour your <b> tags - what you're looking for here is to use the Literal control. Try something like this:

In your markup:

<asp:literal runat="server" id="UploadedFileInfoLiteral" />

In your code-behind:

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
Try
    '' Get the HttpFileCollection
    Dim hfc As HttpFileCollection = Request.Files
    Dim FileInfoStringBuilder As System.Text.StringBuilder = New System.Text.StringBuilder
    Dim hpf As HttpPostedFile

    For i As Integer = 0 To hfc.Count - 1
        hpf = hfc(i)
        If hpf.ContentLength > 0 Then
            hpf.SaveAs(Server.MapPath("/ServerName/DirectoryName") & "\"" & System.IO.Path.GetFileName(hpf.FileName))
            FileInfoStringBuilder.Append("<strong>File: </strong>" & hpf.FileName & " <strong>Size:</strong> " & hpf.ContentLength & " <strong>Type:</strong> " & hpf.ContentType & " Uploaded Successfully! <br/>")
        Else
            FileInfoStringBuilder.Append("Please select a file to upload.")
        End If
    Next

    UploadedFileInfoLiteral.Text = FileInfoStringBuilder.ToString()

Catch ex As Exception

End Try

End Sub
PhilPursglove
Can an HTTP POST filename as reported by a client include invalid Filename characters, such as `"<script>"`?
maxwellb
@maxwellb I don't know, but it'd make for an interesting attack, wouldn't it ;-)
PhilPursglove
PhilPursglove
Yes, I mean, if I sent a POST header with a custom client, which included `"<script>"` in the filename value of my `multipart/form-data`, the file contents would be intact, and probably valid. So, in _this_ case, OP would fail on the `Server.MapPath`, but if the server stores to a BLOB, or an autogenerated server path, and reports the unsanitized "filename" back to the client, there could be a potential attack.
maxwellb
maxwellb