在C Sharp中通过FTP上传PDF文件 [英] Upload PDF files through FTP in C Sharp

查看:189
本文介绍了在C Sharp中通过FTP上传PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码通过ftp上传pdf文件:

I have the following code to upload a pdf file through ftp :

try
        {
            if (!File.Exists(localPath))
                throw new FileNotFoundException(localPath);

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ftpPath));
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Proxy = null;
            request.UseBinary = true;
            request.Credentials = new NetworkCredential(username, password);

            byte[] data = File.ReadAllBytes(localPath);

            using (Stream stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            if (response == null || (response.StatusCode != FtpStatusCode.CommandOK))
                throw new Exception("Upload failed.");
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
            throw e;
        }

我的问题是它仅上载没有图像的文本.我如何不阅读就可以上传文件?我的意思是我只想选择并重命名文件并上传.

My problem that it uploads only text without images . How could i upload a file without reading it? I mean i just want to select and rename the file and upload it.

推荐答案

使用WebClient类,然后使用UploadFile方法. 来自 msdn

Use the WebClient class and use then the UploadFile Method. From msdn

 String uriString = Console.ReadLine();

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
myWebClient.Credentials=new NetworkCredential(username, password);
Console.WriteLine("\nPlease enter the fully qualified path of the file to be uploaded to the URI");
string fileName = Console.ReadLine();
Console.WriteLine("Uploading {0} to {1} ...",fileName,uriString);

// Upload the file to the URI.
// The 'UploadFile(uriString,fileName)' method implicitly uses HTTP POST method.
byte[] responseArray = myWebClient.UploadFile(uriString,fileName);

这篇关于在C Sharp中通过FTP上传PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆