locked
GetPreloadedEntityBody() and ReadEntityBody() problem RRS feed

  • Question

  • User902584711 posted

    Hi.

    I have a problem removing the header/foter information from this methods.

    I just want to save the data.


        private void Application_BeginRequest(Object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpWorkerRequest request = (HttpWorkerRequest)application.Context.GetType().GetProperty("WorkerRequest", (BindingFlags)36).GetValue(application.Context, null);
            
            // Check if it's a multipart/form-data
            if (application.Context.Request.ContentType.IndexOf("multipart/form-data") > -1)
            {
                // Dont know what this does. =) 
                if (request.HasEntityBody())
                {
                    // Create a progress indicator
                    ProgressHandler.SetProgress(SessionID, 0);
    
                    Int32 contentLength = Convert.ToInt32(request.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength));
                    Int32 bytesReceived = 0;
                    Int32 bytesRead = 0;
                    string strRealFilename, strDataFileName;
    
                    // Open a filestream to save the data too..
                    using (FileStream dataFile = new FileStream(@"C:\Inetpub\Share\Junk\" + Guid.NewGuid().ToString() + ".tmp", FileMode.Create))
                    {
                        strDataFileName = dataFile.Name;
                        byte[] body =  request.GetPreloadedEntityBody();
    
                        // Get the real filename from the header.
                        strRealFilename = GetFilenameFromHeader(System.Text.ASCIIEncoding.Default.GetString(body));
                        
                        // Ensure that there is a body
                        if (body != null)
                        {
                            bytesReceived += body.Length;
    
                            /// HERE I WANT TO REMOVE THE HEADER FROM THE body BEFORE SAVING IT TO A FILE!
                            dataFile.Write(body, 0, body.Length);
    
                            // Set the Progress procentage
                            ProgressHandler.SetProgress(SessionID, Divide(bytesReceived, contentLength) * 100);
                        }
    
                        if (request.IsEntireEntityBodyIsPreloaded() == false)
                        {
                            Byte[] buffer = new Byte[BUFF_SIZE];
    
                            while ((contentLength - bytesReceived) >= BUFF_SIZE)
                            {
                                bytesRead = request.ReadEntityBody(buffer, BUFF_SIZE);
                                bytesRead = buffer.Length;
    
                                dataFile.Write(buffer, 0, bytesRead);
                                // HERE I WANT TO REMOVE THE FOOTER IF THIS IS THE LAST PART (Most Unlikly)
                                bytesReceived += bytesRead;
    
                                // Set the Progress procentage
                                ProgressHandler.SetProgress(SessionID, Divide(bytesReceived,contentLength)*100);
    
                                // End this shit if there is no data
                                if (bytesRead == 0)
                                    return;
                            }
    
    
                            bytesRead = request.ReadEntityBody(buffer, (contentLength - bytesReceived));
                            // HERE I WANT TO REMOVE THE FOOTER
                            bytesRead = buffer.Length;
    
                            dataFile.Write(buffer, 0, bytesRead);
                            bytesReceived += bytesRead;
                        }
    
    
                        dataFile.Flush();
                        dataFile.Close();
    
                    } // using (FileStream dataFile 
    
    
                    string strFile = Path.Combine(Path.GetDirectoryName(strDataFileName), Path.GetFileName(strRealFilename));
                    if (!File.Exists(strFile))
                    {
                        
                        File.Move(strDataFileName, strFile);
                    }
                    else
                    {
                        strFile = Path.Combine(Path.GetDirectoryName(strDataFileName), Guid.NewGuid().ToString() + Path.GetExtension(strRealFilename));
                        File.Move(strDataFileName, strFile);
                    }
    
    
                    // Redirect to the page to avoid the browser hanging
                    string current_page = application.Context.Request.CurrentExecutionFilePath;
                    current_page = current_page.Substring(current_page.LastIndexOf("/") + 1) + "Download" + application.Context.Request.QueryString;
                    application.Context.Response.Redirect(current_page);
    
                }
            }
        }



    The big issue is that i have problem parsing the header. I dont realy know any good way.


    Here is an example the begining of a uploaded file will look.

    -----------------------------180071133715457
    Content-Dis-data; name="file_1"; filename="51zJNzyAymL__SS500_.jpg"
    Content-Type: image/jpeg

    ÿØÿà

    Saturday, December 12, 2009 3:32 PM

All replies

  • User78336405 posted

    Hi,

    Please change your post, and put the code in "Insert code". Otherwise we can't open this thread normally.

    Monday, December 14, 2009 1:43 AM
  • User902584711 posted

    I noticed there was some problem and just reposted/edited the post and now it seams to work.

    Monday, December 14, 2009 3:16 AM