1

I have a C#.net web application that can send (by POST method) files to another application. In the second application I have the below code to retrieve the posted file.

HttpPostedFile hpf = Request.Files[0];

Now I can save the file by the code

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

But I need to send it again to another application without saving it here (without saving in 2nd appln I need to send it to a third appln).

(Now I can do is that save the file in second application and from there post it to the third application by giving the path exactly same as what I did in my 1st application. But I need another solution.)

I tried hpf.fileName but its giving only the filename (eg:test.txt). When I tried like below

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);
    }

A WebException occured like 'An exception occurred during a WebClient request.'

Is there any method to do it in C# .net?

Bibhas Debnath
  • 14,559
  • 17
  • 68
  • 96
Sudha
  • 2,078
  • 6
  • 28
  • 53

2 Answers2

2

for creating byte array How to create byte array from HttpPostedFile

Here is a method to save bytes in 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";
}
Community
  • 1
  • 1
Ratna
  • 2,289
  • 3
  • 26
  • 50
  • sorry you can ignore bcode since i pasted it from my library. byte[] f is the byte[] you will get when you will convert the hpf to byte[] . – Ratna Apr 03 '13 at 12:32
  • I will get a stream like this.. Stream inputStream = hpf.InputStream; Can I convert this stream to bytes? – Sudha Apr 03 '13 at 12:48
  • byte[] fileData = null; using (var binaryReader = new BinaryReader(Request.Files[0].InputStream)) { fileData = binaryReader.ReadBytes(Request.Files[0].ContentLength); } //fileData is the byte[] that you will sent to webservice on 3rd application – Ratna Apr 03 '13 at 12:50
  • My file posing code is as follows string url = "the url to be posted"; using (var client = new WebClient()) { byte[] result = client.UploadFile(url, file); string responseAsString = Encoding.Default.GetString(result); } Can I do anything with the Stream object here? – Sudha Apr 03 '13 at 12:51
  • Sorry.. It seems like this code will save the file in the application or it is taking the file which is already in the application and sending it. Isn't it? – Sudha Apr 04 '13 at 05:22
  • But I have the file or file details in hpf from the code HttpPostedFile hpf = Request.Files[0]; and I have the code to send the file is using (var client = new WebClient()) { byte[] result = client.UploadFile(url, file); string responseAsString = Encoding.Default.GetString(result); } . I need to do something with these code. Can I? – Sudha Apr 04 '13 at 05:25
  • It seems like the above code will save the file in the application Isn't it? I want to send it to another application by HTTP POST method. Can I send the fileData in the byte[] format by HTTP POST mrthod? How can I? – Sudha Apr 04 '13 at 08:17
2

Thing is, if you don't want to use web service as suggested in the previous answer, you need to use InputStream property of your HttpPostedFile. You should use HttpWebRequest object to create request with your file content. There are lots of posts and tutorials around including this and this.

Community
  • 1
  • 1
brke
  • 116
  • 3
  • I have only this HttpPostedFile hpf = Request.Files[0]; in my second application. Without saving the file in the second application how can I POST it into a third application? – Sudha Apr 04 '13 at 04:33
  • You should read articles on links in my answer. That's exactly what you need. Especially second one - [this](http://stackoverflow.com/questions/11048258/uploadfile-with-post-values-by-webclient) – brke Apr 04 '13 at 18:35