Coffeehouse Thread

11 posts

My.Computer.Network.UploadFile

Back to Forum: Coffeehouse
  • User profile image
    RamblingGeek​UK

    Hi All,

    I want to upload files from a windows form application and I'm trying to use My.Computer.Network.UploadFile(filename and path, uri)

    But its not working for me, can anyone help?   It's not clear what's required on the server side.

    Many Thanks
    Wayne 

  • User profile image
    JohnAskew

    1. This should be posted in 'Tech Off' forum, not here.

    2. Search engines answer 80% of all programming questions, here's one answer for you: http://msdn.microsoft.com/en-US/library/w3kksch7(v=vs.100).aspx">http://msdn.microsoft.com/en-US/library/w3kksch7(v=vs.100).aspx

    3. Cheers! Search engines are always faster for answers than forums unless no answers are found...

  • User profile image
    RamblingGeek​UK

    Thanks for the reply.   I did find those links myself.   I'm not sure what is required in upload.aspx file, there's no sample/example of what is sent to that page and thus how to handle it.   I have been trying to wrap my head around this all afternoon, hence why I posted to the community for help. 

    I've not posted to c9 for a while, I apologise if I posted in the wrong section.   It would appear that c9 has changed..... Sad

  • User profile image
    JoshRoss

    @JohnAskew: That's the worst MSDN link ever.

    Here is basically what I have done with a WCF webservice,

    Imports System
    Imports System.ServiceModel
    Imports System.ServiceModel.Activation
    Imports System.ServiceModel.Web
    
    Namespace WebService
    
        <ServiceContract()>
        <AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
        <ServiceBehavior(InstanceContextMode:=InstanceContextMode.PerCall)>
        Public Class Service
    
            <WebInvoke(Method:="POST", UriTemplate:="/FileUpload/?fileName={fileName}")> _
            Public Sub UploadFile(fileName As String, input As IO.Stream)
    
                Dim tempPath = Function(s) String.Format("c:\windows\temp\{0}", s)
    
                Using output As New IO.FileStream(tempPath(fileName), IO.FileMode.OpenOrCreate)
                    input.CopyTo(output)
                End Using
    
            End Sub
    
        End Class
    
    End Namespace

  • User profile image
    JoshRoss

    @JoshRoss: This really isn't production code, since it could write whatever into whatever directory by passing in "..\..\blamo.exe" as the file name.

    -Josh

  • User profile image
    DCMonkey

    @Kryptos: I've never used it but UploadFile looks like it uses FTP, so I would guess you need FTP access on the server to where you want to put your file, and maybe use an ftp URI (It's not clear what it does with an http URI. It might just reinterpret it as ftp. I suppose you could enable network tracing to see what it's trying to do).

  • User profile image
    RamblingGeek​UK

    Thanks for the reply.    I've not done anything with WCF.  Will this work with the My.Computer.Network.UploadFile(filename and path, uri)  ?

    Sorry, part time dabbler in the art of programming. 

  • User profile image
    Craig_​Matthews

    , DCMonkey wrote

    @Kryptos: I've never used it but UploadFile looks like it uses FTP, so I would guess you need FTP access on the server to where you want to put your file, and maybe use an ftp URI (It's not clear what it does with an http URI. It might just reinterpret it as ftp. I suppose you could enable network tracing to see what it's trying to do).

    If you're going by the MSDN link that was posted, the only resemblence to FTP is the arbitrary use of "anonymous" as a username. It looks like it sends the data to an upload.aspx page which is supposed to save the file. This functionality is nowhere near FTP. To follow the example of the MSDN link, the OP would need an upload.aspx on a web server listening for this request -- an upload.aspx which isn't explained at all by the posted MSDN link.

  • User profile image
    Sven Groot

    , Craig_Matthews wrote

    *snip*

    If you're going by the MSDN link that was posted, the only resemblence to FTP is the arbitrary use of "anonymous" as a username. It looks like it sends the data to an upload.aspx page which is supposed to save the file. This functionality is nowhere near FTP. To follow the example of the MSDN link, the OP would need an upload.aspx on a web server listening for this request -- an upload.aspx which isn't explained at all by the posted MSDN link.

    The My.Computer.Network.UploadFile method is a convenience wrapper around WebClient.UploadFile with some extra stuff like the ability to show a progress dialog. It uses either FTP or HTTP POST depending on the target URL. The documentation of the WebClient.UploadFile method fortunately does give an example of the server side page if you're using HTTP.

    @JoshRoss: UploadFile has nothing to do with WCF, using a WCF service as the target URL wouldn't work. Also your sample is weird: hardcoded temp path (instead of using Path.GetTempPath), completely pointless use of a lambda (all it does is add unnecessary overhead), use of string.Format where Path.Combine is more appropriate, doesn't prevent escaping the temp directory (as you said).

    I realize it's just a sample, but some adherence to good practice is generally advisable. Don't want to teach the newcomers bad habits, do we? Wink

  • User profile image
    JoshRoss

    @Sven Groot: I used the WFC example, because I've used it to receive files with the HTTP POST verb. I ripped out a lot of gobbly gook for the sample, so things like the hardcoded path seemed more understandable-- from a coffee house post perspective. 

    It seems like a bad idea for the path to even be passed in. Who knows, maybe isolated storage would be a better place for files coming from who-knows-where.

    -Josh

  • User profile image
    RamblingGeek​UK

    The Destination page, needs to deal with accepting the file, here's the code I used: 

     

    <%@ Import Namespace="System"%>
    <%@ Import Namespace="System.IO"%>
    <%@ Import Namespace="System.Net"%>
    <%@ Import NameSpace="System.Web"%>
    
    <Script language="VB" runat=server>
        Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    
            Dim f As String
            Dim file
            For Each f In Request.Files.AllKeys
                file = Request.Files(f)
                file.SaveAs("c:\inetpub\test\UploadedFiles\" & file.FileName)
            Next f
    
        End Sub
    
    </Script>
    <html>
    <body>
    <p> Upload complete. </p>
    </body>
    </html>
     
     

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.