C# 上传文件至远程服务器

C# 上传文件至远程服务器(适用于桌面程序及web程序)  

2009-12-30 19:21:28|  分类: C#|举报|字号 订阅

 
 

最近几天在玩桌面程序,在这里跟大家共享下如何将本地文件上传到远程服务器上面。

注意,我在这里使用的是WebClient而不是ftp

首先,我们先来定义一个类UpLoadFile,这个类就是文件上传类。代码如下:

public void UpLoadFile(string fileNamePath, string uriString, bool IsAutoRename)

        {

            int indexOf = 0;

            if (fileNamePath.Contains(@""))

            {

                indexOf = fileNamePath.LastIndexOf(@"");

            }

            else if (fileNamePath.Contains("/"))

            {

                indexOf = fileNamePath.LastIndexOf("/");

            }

            string fileName = fileNamePath.Substring(indexOf + 1);

            string NewFileName = fileName;

            if (IsAutoRename)

            {

                NewFileName = DateTime.Now.ToString("yyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + fileNamePath.Substring(fileNamePath.LastIndexOf("."));

            }

            string fileNameExt = fileName.Substring(fileName.LastIndexOf(".") + 1);

            if (uriString.EndsWith("/") == false) uriString = uriString + "/";

            uriString = uriString + NewFileName;

            /// 创建WebClient实例

            WebClient myWebClient = new WebClient();

            myWebClient.Credentials = CredentialCache.DefaultCredentials;

            // 要上传的文件

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

            //FileStream fs = OpenFile();

            BinaryReader r = new BinaryReader(fs);

            byte[] postArray = r.ReadBytes((int)fs.Length);

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

            try

            {

                //使用UploadFile方法可以用下面的格式

                //myWebClient.UploadFile(uriString,"PUT",fileNamePath);

                if (postStream.CanWrite)

                {

                    postStream.Write(postArray, 0, postArray.Length);

                    postStream.Close();

                    fs.Dispose();

                   

                }

                else

                {

                    postStream.Close();

                    fs.Dispose();

                   

                }

            }

            catch (Exception err)

            {

                postStream.Close();

                fs.Dispose();            

                throw err;

            }

            finally

            {

                postStream.Close();

                fs.Dispose();

            }

        }

好了,定义好这个类之后就看我们怎么调用它了。在这里我给出一个例子:

单击某个按钮事件:

private void center_Click(object sender, EventArgs e)

{

//上传文件

                //得到文件名,文件扩展名,服务器路径

                string filePath = filename.Text; //需要上传的文件,在这里可以根据需要采用OpenFileDialog来获取文件

                string server = @"http://www.thylx.com/”;   //上传路径

                //创建webclient实例

                WebClient myWebClient = new WebClient();

                try

                {

                    //使用Uploadfile方法上传

                    UpLoadFile(filePath, server, true);

                    MessageBox.Show("上传成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }

                catch (Exception ex)

                {

                    MessageBox.Show(ex.Message);

                    return;

                }

}

这样,我们就可以把选择的文件上传到远程的服务器上面了。

注意:我们配置的服务器的根目录必须在文件系统中具有写入权限,并且在IIS中的配置具有写入权限。

常见错误举例:

405错误:本错误为获取的本地文件路径出现问题或是在远程机不具备写入权限

解决办法:检测文件路径,配置远程服务器中该文件具有写入权限(文件系统跟IIS服务器)

409错误:本错误为远程服务器中不具备此目录

解决办法:在远程机子中新建相应目录即可。

原文地址:https://www.cnblogs.com/alex-13/p/4022908.html