Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i'm new here,

help me out here please,

i am working with web service and doing upload file.

here's my code for uploading file

C#
private void Button_Click(object sender, RoutedEventArgs e)
        {
            testServiceClient = new TestServiceClient();

            var uploadFile = "C:\\Computer1\\Sample.csv";

            try
            {
                var dir = @"\\Computer2\UploadedFile\";
                string myUploadPath = dir;
                var myFileName = Path.GetFileName(uploadFile);

                var client = new WebClient { Credentials = CredentialCache.DefaultNetworkCredentials };

                client.UploadFile(myUploadPath + myFileName, "PUT", uploadFile);
                client.Dispose();

                MessageBox.Show("ok");

                testServiceClient.Close();
            }
            catch (Exception ex)
            {
                ex.ToString();
            }

        }



i can upload file in the same network, but my question is this,

how can i upload file when the two computer is not in the same network?

i've tried changing the

C#
var dir = @"\\Computer2\UploadedFile\";


to

C#
var dir = @"https://Computer2/UploadedFile/";



but i'm getting an error 'unable to connect to remote server'

help me out here pls.


here's my web.config

XML
<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>



and my testservice code is:

C#
public class TestService : ITestService
   {
      public string UploadFile(string fileName)
      {
          return fileName;
      }
   }



sorry, help me pls thanks
Posted
Updated 8-Sep-13 21:31pm
v2
Comments
Sergey Alexandrovich Kryukov 9-Sep-13 3:22am    
Are you sure an HTTP server is running on Computer2 and is configured properly? Do you have a single Web page working for this URL? Are you sure you have the server-side code which can accept uploads? Anyway, you would need to show the server-side code...
—SA
Darryl Louis Montano Rosco 9-Sep-13 3:32am    
sorry, i've updated my question, thanks
Sergey Alexandrovich Kryukov 9-Sep-13 9:05am    
"Not in the same network"?!.. well...
—SA

1 solution

Please try to upload the file with a valid credential. Here is sample piece of code..

WebClient client = new WebClient();
NetworkCredential nc = new NetworkCredential("erandika1986", "123");
Uri addy = new Uri(@"\\192.168.2.4\UploadDocs\"+fileName);
client.Credentials = nc;
byte[] arrReturn = client.UploadFile(addy, textBox1.Text);
MessageBox.Show(arrReturn.ToString());

Hope this helps..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900