时间:2021-05-20
客户端代码:
复制代码 代码如下:
/// <summary>
/// 批量上传图片
/// </summary>
/// <param name="srcurl">服务器路径</param>
/// <param name="imagesPath">图片文件夹路径</param>
/// <param name="files">图片名称</param>
public void UpLoadFile(string srcurl, string imagesPath, List<string> files)
{
int count = 1;
foreach (string imageName in files)
{
string name = imageName;
string url = null;
//+ 加号特殊处理
if (name.Contains("+"))
{
url = srcurl + "name=" + name.Replace("+", "%2B");
}
else
{
url = srcurl + "name=" + name;
}
FileStream fs = new FileStream(imagesPath + name, FileMode.Open);
byte[] data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
fs.Close();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "image/jpeg";
request.Method = "POST";
Encoding encoding = Encoding.UTF8;
request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream(), encoding);
string retString = streamReader.ReadToEnd();
streamReader.Close();
Console.WriteLine((count++) + "/" + files.Count);
}
}
服务器端代码:
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;
using System.IO;
public partial class upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string fPath = Server.MapPath("服务器端图片存储的虚拟目录名称");//得到虚拟目录的真实路径//检查存储目录
if (!Directory.Exists(fPath))
{
Directory.CreateDirectory(fPath);
}
string name = Request.QueryString["name"];//得到文件名
HttpUtility.UrlEncode(name, Encoding.GetEncoding("UTF-8"));
if (name != null)
{
if (!File.Exists(fPath + name))
{
System.IO.Stream stream = Request.InputStream;
byte[] buffer = new byte[stream.Length];
FileStream fs = null;
try
{
fs = new FileStream(fPath + name, FileMode.Create);
while ((stream.Read(buffer, 0, buffer.Length)) > 0)
{
fs.Write(buffer, 0, buffer.Length);
}
}
catch (IOException ioe)
{
Response.Write(ioe);
}
finally
{
if (fs != null)
{
fs.Close();
}
stream.Close();
}
Response.Write(name + "<br>");
Response.Write(File.Exists(fPath + name) + "<br>");
}
}
Response.Write("上传完毕" + Directory.Exists(fPath) + Path.GetFullPath(fPath));
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
C#实现把图片下载到服务器代码ASPX页面代码:
论坛或者贴吧经常会需要分享很多图片,上传图片比较差的做法是上传到中央服务器上,中央服务器再转发给静态图片服务器。而这篇文章讲介绍如何使用plupload对上传过
下面的示例显示如何使用Socket类向HTTP服务器发送数据和接收响应。[C#]publicstringDoSocketGet(stringserver){//
本文实例讲述了C#实现在服务器端裁剪图片的方法。分享给大家供大家参考。具体实现方法如下://图片路径StringoldPath=Server.MapPath("
本文实例为大家分享了js实现上传图片到服务器的具体代码,供大家参考,具体内容如下HTML//多张图片上传multiple//原生提交按钮javascript//