<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分啊。
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分啊。
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 中了,要存储到何处,楼主自己写代码吧
}
}
{
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>