0

I am reading a file from the user files that contains xml that I am processing in a Generic Handler and then passing to the client.

The problem I am having is when I pass the string of xml to the client. Its not in the proper format. It removes the root tag and "<xml 1.0>" tag entirely when looking at it through the client code.

I am looking for some code to preserve the xml string as is when it gets to the client.

I am reading the xml out of a file using System.IO on the server..

public void ProcessRequest(HttpContext context)
{
    if (context.Request.Files.Count > 0)
    {
        string path = context.Server.MapPath("~/Temp");
        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);

        var file = context.Request.Files[0];

        string fileName;

        if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE")
        {
            string[] files = file.FileName.Split(new char[] { '\\' });
            fileName = files[files.Length - 1];
        }
        else
        {
            fileName = file.FileName;
        }

        string strFileName = fileName;
        fileName = Path.Combine(path, fileName);
        file.SaveAs(fileName);
        string msg = File.ReadAllText(fileName);
        File.Delete(fileName);

        context.Response.Write(msg);
    }
}

The xml always starts at "Gambardella..." For some reason it cannot read the beginning of the file when being send to the cient.

Here is an image of the sample xml..

enter image description here

The data is sent out of the handler fine but the client cuts off the top information. It looks like the plugin I am using is storing the (or getting) the data from an iframe. Could the iframe maybe be the culprit in cutting off the beginning xml??

The sample client code I am using is here

2
  • Are you sending the filedata xml as a string to the user? Commented Dec 24, 2012 at 18:30
  • "The xml always starts at "Gambardella..." For some reason it cannot read the beginning of the file when being send to the cient.". I suspect you are using a web browser to view this. View the source - you are probably sending the file as text/html instead as application/xml, meaning the browser will attempt to show it as html. Commented Dec 25, 2012 at 8:58

1 Answer 1

4

You can simply use Response.WriteFile, instead of reading the file then sending it.

Response.WriteFile(fileName);

This will return the contents of the file with the correct HTTP Content-Type header. If the file has the XML declaration, it will not remove it.


Something like the following, based on your code and untested (an without a MemoryStream, as it is not needed in this case):

var file = context.Request.Files[0];
file.InputStream.CopyTo(context.Response.OutputStream)
Sign up to request clarification or add additional context in comments.

14 Comments

The issue is I have to delete the file after I read. In order to read the file I have to save it to a directory on the server, then read it, then delete it. I cannot save Response.WriteFile it returns void
@NickLaMarca - Why write it to disk at all? You can do everything with streams (and MemoryStream in particular).
I am writing to a disk to be able to read the xml out of the file. Is there a better way? all I want to accomplish is get the xml out of the file and send the xml as is back to the client
@NickLaMarca - As I said, if you are creating a string, you can simply create a MemoryStream from it, never writing to disk, and using Response.OutputStream to output it. Like I asked - why are you writing to disk in the first place?
@Nick - I suggest you check the source file then. The code doesn't do anything to the file. If the file does not contain the XML declaration, it will not exist.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.