当前位置:编程学习 > C#/ASP.NET >>

文件上传问题

string Uri = String.Format("http://192.168.1.103/Test/up");//要上传的地址
            string fileName = txtFilePath.Text.Trim(); ;//本地文件的地址
           
            // 创建WebClient实例
            WebClient myWebClient = new WebClient();


            //NetworkCredential credentials = new NetworkCredential("Administrator", "rdserver#123");
            //myWebClient.Credentials = credentials;

            myWebClient.Credentials = CredentialCache.DefaultCredentials; // 添加授权证书
            myWebClient.UseDefaultCredentials = true;
            myWebClient.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
           
            // 要上传的文件
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);

            byte[] postArray = br.ReadBytes((int)fs.Length);
            Stream postStream = myWebClient.OpenWrite(Uri, "PUT");
            if (postStream.CanWrite)
            {
                postStream.Write(postArray, 0, postArray.Length);

            }
            else
            {

            }
            fs.Dispose();
            fs.Close();
            postStream.Dispose();
            postStream.Close();

远程服务器返回错误: (500) 内部服务器错误。
和远程服务器返回错误: (403) 已禁止。
最好一句报错 --------------------编程问答-------------------- iis的权限没有问题! --------------------编程问答-------------------- IIS是否已启用PUT这个谓词.

或者用POST也行. --------------------编程问答--------------------


        private void btnUpload_Click(object sender, EventArgs e)
        {
            string url = "http://localhost/Aspx/upload.ashx";



            MessageBox.Show(Upload(url,txtPath.Text));

        }

        public string Upload(string url, string fileName)
        {
            WebClient webClient = new WebClient();
            byte[] rcv = webClient.UploadFile(url, "POST", fileName);
            return Encoding.UTF8.GetString(rcv);
        }


--------------------编程问答-------------------- 同问,如何检查IIS是否已启用PUT这个谓词.??又如何启用呢?
一样的问题,使用POST的话又会有404未找到的异常 --------------------编程问答-------------------- 用PUT  服务器端 的 WebDAV是否启用了  --------------------编程问答--------------------

这篇文章或许对你有用.

http://www.cnblogs.com/Terrylee/archive/2006/03/27/360165.html
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,