c#上传不了文件到ftp服务器,c#-FTP无法正确上传文件

作者在开发一款个人屏幕捕获应用,能通过快捷键抓取全屏截图并上传至网站。目前遇到的问题是,上传后的图片在网站上显示为损坏状态。代码中涉及到屏幕截图保存、FTP上传以及错误处理部分,但具体原因可能是图片编码或上传过程中的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我正在尝试制作一个小型个人屏幕捕获应用程序,可以在其中按下快捷键,然后上传完整的屏幕截图.

我设法将文件上传到我的网站,但是我遇到的问题是,当您转到URL时,它显示为损坏的图像.

这是我的代码:

private void CaptureFullScreen()

{

string file = DateTime.Now.ToString("ddmmyyyyhhmmss") + ".jpg";

string file_store = screenshotDir + "\" + file;

Rectangle bounds = Screen.GetBounds(Point.Empty);

using(Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))

{

using(Graphics g = Graphics.FromImage(bitmap))

{

g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);

}

bitmap.Save(file_store, ImageFormat.Jpeg);

}

//System.Diagnostics.Process.Start(file);

ShowBalloonTip("Uploading...", "Screen Capture is being uploaded", ToolTipIcon.Info, 1000);

FtpFileUpload(file_store, file);

}

private void FtpFileUpload(string file_store, string file_name)

{

try

{

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://passion4web.co.uk/www/apps/imgcap/" + file_name);

request.Method = WebRequestMethods.Ftp.UploadFile;

request.Credentials = new NetworkCredential("username", "password");

StreamReader strRead = new StreamReader(file_store);

byte[] fileContents = Encoding.UTF8.GetBytes(strRead.ReadToEnd());

strRead.Close();

request.ContentLength = fileContents.Length;

Stream reqStream = request.GetRequestStream();

reqStream.Write(fileContents, 0, fileContents.Length);

reqStream.Close();

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

string url = "http://passion4web.co.uk/apps/imgcap/" + file_name;

string resp = response.StatusDescription;

ShowBalloonTip("Screenshot uploaded", "Click this balloon to open", ToolTipIcon.Info, 5000, url);

response.Close();

}

catch (Exception ex)

{

//Ignore this - used for debugging

MessageBox.Show(ex.ToString(),"Upload error");

MessageBox.Show(file_name + Environment.NewLine + file_store, "Filename, Filestore");

}

}

有任何想法吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值