0

We have a need to chunk-up large http requests sent by our mobile devices. These smaller chunk streams are merged to a file on the server. Once all chunks are received we need a way to submit the saved merged request to an another method(Action) within the same controller that will process this large http request. How can this be done? The code we tried below results in the service hanging. Is there a way to do this without a round-trip?

//Open merged chunked file
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);

//Read steam support variables
int bytesRead = 0;
byte[] buffer = new byte[1024];

//Build New Web Request. The target Action is called "Upload", this method we are in is called "UploadChunk"
HttpWebRequest webRequest;
webRequest = (HttpWebRequest)WebRequest.Create(Request.Url.ToString().Replace("Chunk", string.Empty));
webRequest.Method = "POST";        
webRequest.ContentType = "text/xml";     
webRequest.KeepAlive = true;
webRequest.Timeout = 600000;
webRequest.ReadWriteTimeout = 600000;
webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;

Stream webStream = webRequest.GetRequestStream();  //Hangs here, no errors, just hangs

I have looked into using RedirectToAction and RedirecctToRoute but these methods don't fit well with what we are looking to do as we cannot edit the Request.InputStream (as it is read-only) to carry out large request stream.

gdoron
  • 147,333
  • 58
  • 291
  • 367
Moe Howard
  • 498
  • 7
  • 12

2 Answers2

1

You can build what you want, save in the TempData, RedirectToAction and there pull it from the 'TempData'

something like this:

public ActionResult DoSomething()
{
    var data = GetData();
    TempData["Data"] = data;

    RedirectToAction("Forward");
}

public ActionResult Forward()
{
    var data = TempData["data"];
    if (data == null)
        throw new ArgumentException("data is null");

    // Do your magic.

}    
Community
  • 1
  • 1
gdoron
  • 147,333
  • 58
  • 291
  • 367
  • If we go this route, then the Forward() action would need to parse out the various form fields and file stream bytes for each uploaded file (messy). We were hoping to leverage the framework such that the redirect would look like an original post from the client and we get Request.Form and Request.Files already populated with the merged stream. There is a boat load of logic in the target Action that references the Request object and we would really want to reuse this logic and rather not open a can of worms trying to hook and crook the merged stream data for processing. – Moe Howard Nov 27 '11 at 17:50
  • Whoever voted-up this "answer" clearly didn't read the complete problem-set. – Moe Howard Nov 28 '11 at 17:13
0

Found what I was looking for here: Needed to perform the following steps:

1) Create File stream to read the merged chunk from file
2) Create new System.Net.HttpWebRequest with destination URI
3) Copy Header from original request to new requested noted in 2)
4) Copy contents from file stream to new request
5) Set the content-lenght of new request to the lenght of the file stream
6) Close both file stream and new request streams
7) Copy Headers from original to new request
8) Copy Cookies from original to new request


So basically its the same logic one could use when programming ASP.NET stuff.

Community
  • 1
  • 1
Moe Howard
  • 498
  • 7
  • 12