使用IIS作文件服务,使用C#实现上传删除
最近做一个项目,合作方使用了IIS作为文件服务器.
合作方给我们配置了相关信息:
一个http ULR,用户名,密码;告诉我们只要通过HTTP就可以实现文件上传
由于合作方的服务器我们是操作不了的..仅仅知道是IIS作服务器;
找了很多资料费了九牛二虎之力才知道如果通过IIS来上传文件.特写下来备忘.
搜索了很久没有找到很多相关的资料,最后在微软的网站看到一篇英文的文章,自己再慢慢测试.终于解决.
主要是利用了HTTP1.1协议支持的PUT/DELETE 操作.平时我们常见的只是GET或者POST ..
具体操作见下面:
IIS配置服务器:
1.在IIS中新建立个站点,(详细操作略过)
2.右键站点=>属性=>主目录=>把"写入"勾上
3.在"IISweb服务扩展" ,把 "WEBDAV"允许了(我由于没的选上这个,测试了半天都不行)
4.站点的目录中,把EVERYONE配置为可完全操作(为了安全,你可以只允许某个用户完全控制;等一会用这个用户来上传/删除)
在C#中上传文件:
代码如下:
publicvoid UploadFileBinary(string localFile,string uploadUrl) { HttpWebRequest req= (HttpWebRequest)WebRequest.Create(uploadUrl); req.Credentials= new NetworkCredential("Administrator","123456");//用户名,密码 req.PreAuthenticate= true; req.Method= "PUT"; req.AllowWriteStreamBuffering= true; // Retrieve request stream Stream reqStream= req.GetRequestStream(); // Open the local file FileStream rdr= new FileStream(localFile, FileMode.Open); // Allocate byte buffer to hold file contents byte[] inData= newbyte[4096]; // loop through the local file reading each data block // and writing to the request stream buffer int bytesRead= rdr.Read(inData,0, inData.Length); while (bytesRead> 0) { reqStream.Write(inData,0, bytesRead); bytesRead= rdr.Read(inData,0, inData.Length); } rdr.Close(); reqStream.Close(); req.GetResponse(); }
删除文件的代码 :
public void DeleteFile(string uploadUrl) { HttpWebRequest req= (HttpWebRequest)WebRequest.Create(uploadUrl); req.Credentials= new NetworkCredential("Administrator","123456"); req.PreAuthenticate= true; req.Method= "DELETE"; //req.AllowWriteStreamBuffering = true; req.GetResponse(); } 注:当上传是返回 远程服务器返回错误: (409) 冲突。错误时请注意,是由于服务器上不存在上传文件设定的目录,而webclient不会自动创建文件夹所导致。手工创建对应的文件夹即可。(http://topic.csdn.net/u/20110307/11/947f934a-6ecc-4be1-a4b8-64612cc648db.html)
我自己的代码:
上传:
string wtfw_id = System.Guid.NewGuid().ToString();//FJ_CQ_ID随机 string fjqmm = System.Guid.NewGuid().ToString() + System.IO.Path.GetExtension(this.FileUpload1.FileName);//附件全名 string fjhz = System.IO.Path.GetExtension(this.FileUpload1.FileName);//附件后缀 string Uri = String.Format(ConfigurationManager.AppSettings["WTFWDZ"].ToString(), fjqmm);//要上传的地址 string fileName = FileUpload1.PostedFile.FileName;//本地文件的地址 // 创建WebClient实例 WebClient myWebClient = new WebClient(); //访问权限设置 myWebClient.Credentials = CredentialCache.DefaultCredentials; // 要上传的文件 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 { } postStream.Close();
删除:
public static void DelFileBinary(string DelUrl) { //DelUrl 删除文件的地址和文件的名字 // 创建WebClient实例 WebClient myWebClient = new WebClient(); //访问权限设置 myWebClient.Credentials = CredentialCache.DefaultCredentials; try { byte[] postArray = new byte[] { }; Stream postStream = myWebClient.OpenWrite(DelUrl, "DELETE"); if (postStream.CanWrite) { postStream.Write(postArray, 0, 0); } else { } postStream.Close(); } catch (WebException errMsg) { // label1.Text = "上传失败:" + errMsg.Message; } }