一切福田,不離方寸,從心而覓,感無不通。

在asp.net中发送图片到远程服务器上

发送代码:

var imgFile = Request.Files["imgFile"];
if (imgFile == null) return;

var img = Image.FromStream(imgFile.InputStream);
var ms = new MemoryStream();
img.Save(ms,ImageFormat.Jpeg);

var data = new byte[ms.Length];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(data, 0, Convert.ToInt32(ms.Length));
ms.Dispose();

var fileName = imgFile.FileName;
var fileType = imgFile.ContentType;
var fileSize = data.Length.ToString();

var myRequest = WebRequest.Create(Common.ImgUrl + "/upload/UploadImg.aspx");
myRequest.Method = "POST";
myRequest.ContentType = fileType;
myRequest.ContentLength = data.Length;
myRequest.Headers.Add("FileType", Server.UrlEncode(fileType));
myRequest.Headers.Add("FileSize", fileSize);
myRequest.Headers.Add("FileName", Server.UrlEncode(fileName));
myRequest.Headers.Add("dir", Server.UrlEncode(Request["dir"]));

using (var newStream = myRequest.GetRequestStream())
{
// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();
}

// Get response
var myResponse = myRequest.GetResponse();
var reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
var content = reader.ReadToEnd();
Response.Write(content);

 

接收代码:(因为我是用在kindeditor中的,所以接收接收方式和返回结果不懂同学参考kindeditor)

protected void Page_Load(object sender, EventArgs e)
{
var path = "/";

var fileName = Server.UrlDecode(Request.Headers["FileName"]);
var fileSize = int.Parse(Request.Headers["FileSize"]);
var dirName = Server.UrlDecode(Request.Headers["dir"]);
var fileExt = Path.GetExtension(fileName);
fileExt = string.IsNullOrEmpty(fileExt) ? ".jpg" : fileExt.ToLower();

var newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;

var jss = new JavaScriptSerializer();
var extTable = new Hashtable
{
{"image", "gif,jpg,jpeg,png,bmp"},
{"flash", "swf,flv"},
{"media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb"},
{"file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2"}
};

//最大文件大小
const int maxSize = 1000000;
if (fileSize < 1)
{
ShowError("请选择文件。");
}

if (String.IsNullOrEmpty(dirName))
{
dirName = "image";

}
if (!extTable.ContainsKey(dirName))
{
ShowError("目录名不正确。");
}

if (fileSize > maxSize)
{
ShowError("上传文件大小超过限制。");
}

if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1)
{
ShowError("上传文件扩展名是不允许的扩展名。\n只允许" + ((String)extTable[dirName]) + "格式。");
}

//path
switch (dirName)
{
case "image":
path += "img";
break;
case "media":
case "flash":
path += "swf";
break;
case "file":
path += "doc";
break;
}
path += "/";
var ymd = DateTime.Now.ToString("yyyyMM", DateTimeFormatInfo.InvariantInfo);
path += ymd;

var savePath = Server.MapPath(path);
var filePath = Server.MapPath(path + "/" + newFileName);
var fileUrl = Common.ImgUrl + path + "/" + newFileName;
if (!Directory.Exists(savePath))
{
Directory.CreateDirectory(savePath);
}

//保存文件
var bytes = Request.BinaryRead(fileSize);
File.WriteAllBytes(filePath, bytes);

var hash = new Hashtable();
hash["error"] = 0;
hash["url"] = fileUrl;
Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
Response.Write(jss.Serialize(hash));
Response.End();
}

private void ShowError(string message)
{
var hash = new Hashtable();
hash["error"] = 1;
hash["message"] = message;
Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
Response.Write(new JavaScriptSerializer().Serialize(hash));
Response.End();
}