下面就为您介绍C#实现上传下载图片的完整攻略。
1. 上传图片
1.1 上传图片的基本原理
上传图片其实是将本地计算机的图片文件传输到服务器上。由于HTTP协议是无状态的,因此可以使用POST请求将文件传输到服务器上。
1.2 上传图片的技术实现
在C#中进行上传图片的技术实现,可以通过以下步骤完成:
1.2.1 选择需要上传的图片文件
使用OpenFileDialog控件选择需要上传的图片文件,并获取图片文件的本地路径。
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "图片文件|*.jpg;*.gif;*.png";
if (dialog.ShowDialog() == DialogResult.OK)
{
string imagePath = dialog.FileName;
}
1.2.2 将图片文件以二进制流的形式进行上传
使用HttpWebRequest对象,构造POST请求,并设置请求头信息,将本地图片文件以二进制流的形式上传到服务器上。
string url = "http://localhost:8080/upload";
string imagePath = "D:\\test.jpg";
string boundary = "----" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "multipart/form-data; boundary=" + boundary;
// 设置请求头信息
StringBuilder headerBuilder = new StringBuilder();
headerBuilder.AppendLine("--" + boundary);
headerBuilder.AppendLine("Content-Disposition: form-data; name=\"imageFile\"; filename=\"" + Path.GetFileName(imagePath) + "\"");
headerBuilder.AppendLine("Content-Type: application/octet-stream");
headerBuilder.AppendLine();
byte[] headerBytes = Encoding.UTF8.GetBytes(headerBuilder.ToString());
byte[] footerBytes = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");
// 构造二进制流
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(headerBytes, 0, headerBytes.Length);
using (FileStream fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[1024 * 1024];
int readLength = 0;
while ((readLength = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
requestStream.Write(buffer, 0, readLength);
}
}
requestStream.Write(footerBytes, 0, footerBytes.Length);
}
// 获取服务器返回信息
using (WebResponse response = request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader responseReader = new StreamReader(responseStream, Encoding.UTF8))
{
string responseText = responseReader.ReadToEnd();
}
}
}
1.3 上传图片的实例
下面是一个上传图片的示例:
private void UploadButton_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "图片文件|*.jpg;*.gif;*.png";
if (dialog.ShowDialog() == DialogResult.OK)
{
string url = "http://localhost:8080/upload";
string imagePath = dialog.FileName;
string boundary = "----" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "multipart/form-data; boundary=" + boundary;
StringBuilder headerBuilder = new StringBuilder();
headerBuilder.AppendLine("--" + boundary);
headerBuilder.AppendLine("Content-Disposition: form-data; name=\"imageFile\"; filename=\"" + Path.GetFileName(imagePath) + "\"");
headerBuilder.AppendLine("Content-Type: application/octet-stream");
headerBuilder.AppendLine();
byte[] headerBytes = Encoding.UTF8.GetBytes(headerBuilder.ToString());
byte[] footerBytes = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(headerBytes, 0, headerBytes.Length);
using (FileStream fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[1024 * 1024];
int readLength = 0;
while ((readLength = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
requestStream.Write(buffer, 0, readLength);
}
}
requestStream.Write(footerBytes, 0, footerBytes.Length);
}
using (WebResponse response = request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader responseReader = new StreamReader(responseStream, Encoding.UTF8))
{
string responseText = responseReader.ReadToEnd();
}
}
}
}
}
2. 下载图片
2.1 下载图片的基本原理
下载图片其实是将服务器上的图片文件下载到本地计算机。由于HTTP协议是无状态的,因此可以使用GET请求将服务器上的文件传输到本地计算机上。
2.2 下载图片的技术实现
在C#中进行下载图片的技术实现,可以通过以下步骤完成:
2.2.1 获取服务器图片文件的基本信息
使用HttpWebRequest对象,构造GET请求,并获取服务器图片文件的基本信息,例如文件名、文件类型等。
string imageUrl = "http://localhost:8080/images/test.jpg";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imageUrl);
request.Method = "HEAD";
using (WebResponse response = request.GetResponse())
{
string fileName = response.Headers["Content-Disposition"].Substring(response.Headers["Content-Disposition"].IndexOf("=") + 1);
string fileType = response.ContentType;
long fileSize = response.ContentLength;
}
2.2.2 将图片文件以二进制流的形式进行下载
使用HttpWebRequest对象,构造GET请求,并以二进制流的形式下载服务器上的图片文件。
string imageUrl = "http://localhost:8080/images/test.jpg";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imageUrl);
request.Method = "GET";
using (WebResponse response = request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (MemoryStream memoryStream = new MemoryStream())
{
byte[] buffer = new byte[1024 * 1024];
int readLength = 0;
while ((readLength = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
memoryStream.Write(buffer, 0, readLength);
}
byte[] imageBytes = memoryStream.ToArray();
}
}
}
2.3 下载图片的实例
下面是一个下载图片的示例:
private void DownloadButton_Click(object sender, EventArgs e)
{
string imageUrl = "http://localhost:8080/images/test.jpg";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imageUrl);
request.Method = "HEAD";
using (WebResponse response = request.GetResponse())
{
string fileName = response.Headers["Content-Disposition"].Substring(response.Headers["Content-Disposition"].IndexOf("=") + 1);
string fileType = response.ContentType;
long fileSize = response.ContentLength;
request = (HttpWebRequest)WebRequest.Create(imageUrl);
request.Method = "GET";
using (WebResponse imageResponse = request.GetResponse())
{
using (Stream responseStream = imageResponse.GetResponseStream())
{
using (MemoryStream memoryStream = new MemoryStream())
{
byte[] buffer = new byte[1024 * 1024];
int readLength = 0;
while ((readLength = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
memoryStream.Write(buffer, 0, readLength);
}
byte[] imageBytes = memoryStream.ToArray();
using (SaveFileDialog dialog = new SaveFileDialog())
{
dialog.FileName = fileName;
dialog.Filter = fileType + "|*" + Path.GetExtension(fileName);
if (dialog.ShowDialog() == DialogResult.OK)
{
using (FileStream fileStream = new FileStream(dialog.FileName, FileMode.Create))
{
fileStream.Write(imageBytes, 0, imageBytes.Length);
}
}
}
}
}
}
}
}
以上就是C#实现上传下载图片的完整攻略,希望能对您有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#实现上传下载图片 - Python技术站