使用WebClient上传文件的两种方式

时间:2022-09-16 17:48:11

方式一: 

 string paths = "~/img/baidu_logo.gif";
        ImageButton1.ImageUrl = GetConfig.GetPhotoPath(paths);

        System.Net.WebClient myWebClient = new System.Net.WebClient();
 
        string uriString = "http://192.168.1.200/hzyl/Photo/123.bmp";
        string fileName = @"c://371311198802243458.bmp";

         myWebClient.UploadFile(uriString, "put", fileName);

 

  myWebClient.Dispose();

 

 

方式二:

 

 

         string uriString = "http://localhost/FileUpLoad/2006327143303_Grid1.jpg";
       
        // Local Directory File Info
        string fileName = @"c:/temp/2006327143303_Grid1.jpg";

        // Create a new WebClient instance.
        WebClient myWebClient = new WebClient();

        FileStream fs = new FileStream(fileName,FileMode.Open,FileAccess.Read);

        BinaryReader br = new BinaryReader(fs);

        Byte[] postArray = br.ReadBytes(Convert.ToInt32(fs.Length));

        Stream postStream = myWebClient.OpenWrite(uriString,"PUT");

        if(postStream.CanWrite)
        {
            postStream.Write(postArray,0,postArray.Length);
        }
        postStream.Close();
        fs.Close();

  myWebClient.Dispose();

 

 

如果弹出了“远程服务器返回错误: (501) 未实现”的错误,说明Web服务扩展中的WebDAV没有打开

使用WebClient上传文件的两种方式