日期:2014-05-16  浏览次数:20905 次

apache 使用put方法进行文件上传
最近在写一个C#的应用程序,想实现向自己的ubuntu服务器上传文件

使用put方法:
public bool uploadfile(string netLocation, string localPath, ref string retString)
        {
            bool retState = false;
            
            string tmp = "http://" + serverIP + "/" + netLocation;
            try
            {
                WebClient myWebClient = new WebClient();

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

                BinaryReader br = new BinaryReader(fs);

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

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

                if (postStream.CanWrite)
                {
                    postStream.Write(postArray, 0, postArray.Length);
                }                postStream.Close();
                fs.Close();
                retString = "上传成功";
                retState = true;
            }
            catch (Exception ex)
            {
                retState = false;
                retString = tmp.Trim() + ":" + ex.Message;
            }
            return retState;
        }

    wClient.UploadFile(tmp.Trim(), "PUT", localPath);
 提示:
    远程服务器返回错误: (405) 不允许的方法;

查了资料说,apache安装默认不支持put和delete方法,需要修改配置文件

于是修改httpd.conf,添加:
LoadModule dav_module modules/mod_dav.so
LoadModule dav_fs_module modules/mod_dav_fs.so