在C#中处理HttpPostedFile

我有一个C#.net Web应用程序,可以(通过POST方法)文件发送到另一个应用程序。 在第二个应用程序中,我有以下代码来检索已发布的文件。

HttpPostedFile hpf = Request.Files[0]; 

现在我可以通过代码保存文件了

 hpf.SaveAs("The path to be saved"); 

我需要将它再次发送到另一个应用程序而不保存在这里 (没有保存在第二个appln我需要将它发送到第三个应用程序)。

(现在我可以做的是将文件保存在第二个应用程序中,然后通过提供与我在第一个应用程序中完全相同的路径将其发布到第三个应用程序。但我需要另一个解决方案。)

我尝试了hpf.fileName但它只给出了文件名(例如:test.txt)。 当我尝试下面的时候

 string file = hpf.FileName; string url = "the url to send file"; using (var client = new WebClient()) { byte[] result = client.UploadFile(url, file); string responseAsString = Encoding.Default.GetString(result); } 

发生WebException,如“WebClient请求期间发生exception”。

在C#.net中有没有办法做到这一点?

事情是,如果您不想使用上一个答案中建议的Web服务,则需要使用HttpPostedFile的InputStream属性。 您应该使用HttpWebRequest对象来创建包含文件内容的请求。 有很多post和教程,包括这个和这个 。

用于创建字节数组如何从HttpPostedFile创建字节数组

这是一种在webservice中保存字节的方法

 [WebMethod] public string UploadFile(byte[] f, string fileName, string bcode) { if (bcode.Length > 0) { try { string[] fullname = fileName.Split('.'); string ext = fullname[1]; if (ext.ToLower() == "jpg") { MemoryStream ms = new MemoryStream(f); FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath("~/bookimages/zip/") + bcode+"."+ext, FileMode.Create); ms.WriteTo(fs); ms.Close(); fs.Close(); fs.Dispose(); } else { return "Invalid File Extention."; } } catch (Exception ex) { return ex.Message.ToString(); } } else { return "Invalid Bookcode"; } return "Success"; }