uploadfile窗口形成C#Web服务 [英] uploadfile windows form C# web service

查看:95
本文介绍了uploadfile窗口形成C#Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的,



请帮帮我,



i我正在与之合作网络服务和上传文件。



这是我上传文件的代码



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

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可以上传文件相同的网络,但我的问题是这个,



如果两台计算机不在同一个网络中,我怎样才能上传文件?



我试过更改






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

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





to





to

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







但我收到错误'无法连接到远程服务器'


帮我解决这个问题。





这是我的web.config






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

help me out here pls.


here's my web.config

<?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>







和我的testservice代码是:






and my testservice code is:

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







抱歉,帮帮我谢谢




sorry, help me pls thanks

推荐答案

请尝试使用有效凭证上传文件。以下是示例代码..



WebClient客户端=新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());



希望这会有所帮助..
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..


这篇关于uploadfile窗口形成C#Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆