httpwebrequest上传图片问题[100分]

xmsheji 2009-10-03 08:29:57
 <form action="http://192.168.1.104:8088/schedule/Upload" method="post" enctype="multipart/form-data"
name="upload">
<input type="file" name="file" id="file">
<input type="submit">
</form>



谁能给一段Httpwebrequest上传图片的例子,主要是怎么对文件进行处理和编码,这个不太清楚,(文字已经可以上传了), 模拟上面这个表单。

成功马上给分啊!

http://topic.csdn.net/u/20090930/15/23a11827-e9bd-46bb-ba17-8c6ed2aac10c.html
另外这个帖子发错了类别,加上这50分,共100分啊。
...全文
499 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
maole413149 2010-05-10
  • 打赏
  • 举报
回复
up
..学习
孟子E章 2009-10-04
  • 打赏
  • 举报
回复
C#代码

    private void button1_Click(object sender, EventArgs e)
{
String fileToUpload = "E:\\bjxml20071016004.jpg";
String uploadUrl = "http://localhost/ask/UploadFile.aspx";
String fileFormName = "file";
String contenttype = "image/jpeg";
string boundary = "----------" + DateTime.Now.Ticks.ToString("x");


HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uploadUrl);
webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
webrequest.Method = "POST";
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"");
sb.Append(fileFormName);
sb.Append("\"; filename=\"");
sb.Append(Path.GetFileName(fileToUpload));
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: ");
sb.Append(contenttype);
sb.Append("\r\n");
sb.Append("\r\n");

string postHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
FileStream fileStream = new FileStream(fileToUpload,FileMode.Open, FileAccess.Read);
long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
webrequest.ContentLength = length;
Stream requestStream = webrequest.GetRequestStream();
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

byte[] buffer = new Byte[(int)fileStream.Length];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
requestStream.Write(buffer, 0, bytesRead);
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
requestStream.Close();
}



接收端的代码:
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
HttpFileCollection files = HttpContext.Current.Request.Files;
for (int iFile = 0; iFile < files.Count; iFile++)
{
HttpPostedFile postedFile = files[iFile];
string fileName;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("~/") + fileName);
}
}

}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit" />
</form>
</body>
</html>
zming 2009-10-04
  • 打赏
  • 举报
回复
客户端页面:

<form action="http://192.168.1.104:8088/schedule/Upload" method="post" enctype="multipart/form-data"
name="upload">
<input type="file" name="file" id="file">
<input type="submit">
</form>


服务器ASP.NET代码:

protected void Page_Load(object sender, EventArgs e)
{
HttpFileCollection httpFiles = Request.Files;
for (int i = 0; i < httpFiles.Count; i++)
{
string filename = httpFiles[i].FileName; // 上传文件名称
int filesize = httpFiles[i].ContentLength; // 上传文件大小
string mimetype = httpFiles[i].ContentType; // 上传文件的MIME类型, 二进制数据应该为"application/octet-stream"

byte[] filedata = new byte[filesize];
httpFiles[i].InputStream.Read(filedata, 0, filesize);

// 上传文件的数据都读到 filedata 中了,要存储到何处,楼主自己写代码吧
}
}
mngzilin 2009-10-04
  • 打赏
  • 举报
回复
Httpwebrequest在服务端请求,你要上传图片的用HttpPostedFile
宝_爸 2009-10-03
  • 打赏
  • 举报
回复
你是说AJAX吗?
xmsheji 2009-10-03
  • 打赏
  • 举报
回复
大家来帮帮忙,难道没做这个的吗

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧