我正在编写一个C#ASP.Net应用程序,供客户端将文件发布到其他服务器。我正在使用通用处理程序来处理从客户端到服务器的已发布文件。但是在我的处理程序中,context.Request.Files总是为空(0计数)。我相信我的post方法是正确的,因为当我尝试在与客户端相同的域中移动处理程序时,我可以接受这些文件并保存它们。但问题是我需要将文件保存到其他服务器。
以下是发布文件的代码:
private void UploadFilesToRemoteUrl3(HttpFileCollection files)
{
string url = "http://localhost:19107/Catalog/api/dashboard/ImageHandler.ashx";
long length = 0;
string boundary = "----------------------------" +
DateTime.Now.Ticks.ToString("x");
HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
httpWebRequest2.Method = "POST";
httpWebRequest2.KeepAlive = true;
httpWebRequest2.Credentials =
System.Net.CredentialCache.DefaultCredentials;
Stream memStream = new System.IO.MemoryStream();
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
memStream.Write(boundarybytes,0,boundarybytes.Length);
length += boundarybytes.Length;
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";
foreach (string s in files)
{
HttpPostedFile file = files[s];
string header = string.Format(headerTemplate, "file", file.FileName);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes,0,headerbytes.Length);
length += headerbytes.Length;
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ( (bytesRead = file.InputStream.Read(buffer, 0, buffer.Length)) != 0 )
{
memStream.Write(buffer, 0, bytesRead);
length += bytesRead;
}
memStream.Write(boundarybytes,0,boundarybytes.Length);
length += boundarybytes.Length;
file.InputStream.Close();
}
httpWebRequest2.ContentLength = memStream.Length;
Stream requestStream = httpWebRequest2.GetRequestStream();
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer,0,tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer,0,tempBuffer.Length );
requestStream.Close();
WebResponse webResponse2 = httpWebRequest2.GetResponse();
Stream stream2 = webResponse2.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
string a = reader2.ReadToEnd();
webResponse2.Close();
httpWebRequest2 = null;
webResponse2 = null;
}
这是接收文件的处理程序背后的代码:
public void ProcessRequest(HttpContext context)
{
context.Request.ContentType = "multipart/form-data";
int count = context.Request.Files.Count; //always 0
foreach (string s in context.Request.Files)
{
string response = "";
HttpPostedFile file = context.Request.Files[s];
//code to save files
}
}
答案 0 :(得分:0)
public Response<List<string>> UplaoadPostImage()
{
Response<List<string>> response = new Response<List<string>>();
ResponseImage objtemp = new ResponseImage();
List<string> objlist = new List<string>();
try
{
HttpContextWrapper objwrapper = GetHttpContext(this.Request);
HttpFileCollectionBase collection = objwrapper.Request.Files;
if (collection.Count > 0)
{
foreach (string file in collection)
{
HttpPostedFileBase file1 = collection.Get(file);
Stream requestStream = file1.InputStream;
Image img = System.Drawing.Image.FromStream(requestStream);
//Image UserImage = objcommon.ResizeImage(img, 600, 600);
string UniqueFileName = file1.FileName;
img.Save(HttpContext.Current.Request.PhysicalApplicationPath + "UploadImage\\" + UniqueFileName, System.Drawing.Imaging.ImageFormat.Png);
objlist.Add(ConfigurationManager.AppSettings["ImagePath"] + UniqueFileName);
requestStream.Close();
}
response.Create(true, 0, Messages.FormatMessage(Messages.UploadImage_Sucess, ""), objlist);
}
else
{
response.Create(false, 0, "File not found.", objlist);
}
}
catch (Exception ex)
{
response.Create(false, -1, Messages.FormatMessage(ex.Message), objlist);
}
return response;
}
private HttpContextWrapper GetHttpContext(HttpRequestMessage request = null)
{
request = request ?? Request;
if (request.Properties.ContainsKey("MS_HttpContext"))
{
return ((HttpContextWrapper)request.Properties["MS_HttpContext"]);
}
else if (HttpContext.Current != null)
{
return new HttpContextWrapper(HttpContext.Current);
}
else
{
return null;
}
}