1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Edit Remote Text File with WebClient?

Discussion in 'Visual Basic .NET' started by fpforum, Dec 28, 2012.

  1. fpforum

    fpforum Junior Member

    Joined:
    Apr 22, 2008
    Messages:
    117
    Likes Received:
    4
    Home Page:
    Hey everyone..What I am trying to do is write some information to a text file that I have stored on my server, not locally.

    I've been looking around and can't find much that talks about writing to a remote text file inside vb express. I've given the text file full write access so it shouldn't have any problem writing to it..But streamwriters File.AppendText will only work with local files.

    Also, I'm not looking to write over the file everytime..Similar to appendtext, I am looking to write a line and then have it add onto that everytime.

    Thanks!
     
  2. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,143
    You can't do that directly.

    WebClient sends text -> Script on the server writes it on file
     
  3. fpforum

    fpforum Junior Member

    Joined:
    Apr 22, 2008
    Messages:
    117
    Likes Received:
    4
    Home Page:
    Know where I could find any examples?

    Thanks!!
     
  4. Psychop1

    Psychop1 Registered Member

    Joined:
    Jul 30, 2010
    Messages:
    63
    Likes Received:
    19
    • Thanks Thanks x 1
  5. fpforum

    fpforum Junior Member

    Joined:
    Apr 22, 2008
    Messages:
    117
    Likes Received:
    4
    Home Page:
    Thanks Psychop1, I will check that out. After I was looking further into this I learned that you can't really write to a text file remotely without a web service...What I would need to do is download the file, append the line, and then reupload it. I'll check this OpenWrite out.

    Thanks!

    ------------------------------

    Update..I've tried this code out and it doesn't actually write to the text file on my server..Here is the code I'm using

    Code:
        Public Sub ShareIt()
            Dim uriString As New Uri("http://mydomain.com/text.txt")
            
            Dim postData As String = (singledomain & registrar & Now & vbCrLf)
    
            ' Apply ASCII Encoding to obtain an array of bytes .
            Dim postArray As Byte() = Encoding.ASCII.GetBytes(postData)
    
            ' Create a new WebClient instance.
            Dim myWebClient As New WebClient()
    
            'Console.WriteLine("Uploading to {0} ...", uriString)
    
            ' OpenWrite implicitly sets HTTP POST as the request method.
            Dim postStream As Stream = myWebClient.OpenWrite(uriString)
            postStream.Write(postArray, 0, postArray.Length)
    
            ' Close the stream and release resources.
            postStream.Close()
    
            MsgBox("Shared Successfully!")
            'Console.WriteLine(ControlChars.Cr + "Successfully posted the data.")
        End Sub
    
    It doesn't throw any errors..but it doesn't write anything either.
     
    Last edited: Dec 28, 2012
  6. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,143
    That 's because what it does is send the data to the server - if there 's no script on the server to do something with it, what do you expect to happen? :) The server gets the data and discards it, that 's why you get no error but nothing happens.
     
  7. fpforum

    fpforum Junior Member

    Joined:
    Apr 22, 2008
    Messages:
    117
    Likes Received:
    4
    Home Page:
    Yea jazz this is where the web service would need to come into place, correct?
     
  8. gimme4free

    gimme4free Executive VIP Jr. VIP Premium Member

    Joined:
    Oct 22, 2008
    Messages:
    1,879
    Likes Received:
    1,931
    Upload a file to your server then POST the data to that file, E.g.
    PHP:
    <?php
    $new_contents 
    $_REQUEST['content'];
    $fp fopen("filename.txt","w");
    fwrite($fp,$new_contents);
    fclose($fp);
    ?>
    Also note that this will allow ANY user to run this file so it is not secure, you may want to u/p protect it or really obfuscate the filename or deny access to IP's other than your own by .htaccess to secure.
     
    • Thanks Thanks x 1