views:

443

answers:

3

I am developing a webpage in that the user can download their Resume for Edit. So I have a link to download the article. I use the following code for download.

DataTable dt = user.getUserDetails(2);
user.WriteFileFromDBbyUserArticleID(Server.MapPath(Convert.ToString(dt.Rows[0].ItemArray[0])), Convert.ToInt32(2), "CV");
FileUtil.writeFileToResponse(Server.MapPath(Convert.ToString(dt.Rows[0].ItemArray[0])), Response);

////////////////////////////////////////////////
public void WriteFileFromDBbyUserArticleID(string FilePath, int UserID, string FileType)
{
    DataAccessLayer dal = new DataAccessLayer();

    string selectQuery = "Select Articles.Users_WriteFileFromDB(?,?,? ) from Articles.Users";

    DbParameter[] parm = new DbParameter[3];
    parm[0] = dal.GetParameter();
    parm[0].ParameterName = "@FilePath";
    parm[0].Value = FilePath;

    parm[1] = dal.GetParameter();
    parm[1].ParameterName = "@UserID";
    parm[1].Value = UserID;

    parm[2] = dal.GetParameter();
    parm[2].ParameterName = "@FileType";
    parm[2].Value = FileType;

    DataTable dtArticleStatus = dal.ExecuteDataTable(selectQuery, parm);
}

///////////////////////////////////////////////////////////////////////////
static public void writeFileToResponse(string filePath,HttpResponse Response)
{
    try
    {
        string FileName = Path.GetFileName(filePath);
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
        Response.WriteFile(filePath);
        Response.Flush();
        File.Delete(filePath);
        Response.End();
    }
        catch (System.Exception ex)
    {
    }
}

I got the error in the line "Response.WriteFile(filePath);" as follows

sys.webforms.pagerequestManagerparserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to response.write(), response filters, httpModules, or server trace is enabled. Details:Error parsing near ...

How do I fix this?

A: 

You can stream the document back using Response stream.

This might get you going.

Response

Request, Response Objects

astander
A: 

Are you using ASP.NET? Try HtmlInputFile for the uploads.

http://www.codeproject.com/KB/aspnet/fileupload.aspx

Mark Byers
Seems to be asking about edit, not input.
astander
Hmm, I was going mostly by the title since the question was a bit confusing, but reading it again it seems you might be right. Either way, leaving this answer here will help oher users that find this question from the title.
Mark Byers
+1  A: 
public class FileHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        if (context.Request["file"] != null)
        {
            try
            {
                string file = context.Server.MapPath("~/files/" + context.Request["file"].ToString());
                FileInfo fi = new FileInfo(file);
                if (fi.Exists)
                {
                    context.Response.ClearContent();
                    context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fi.Name);
                    context.Response.AddHeader("Content-Length", fi.Length.ToString());
                    string fExtn = "video/avi";
                    context.Response.ContentType = fExtn;
                    context.Response.TransmitFile(fi.FullName);
                    context.Response.End();
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }
    }

    public bool IsReusable
    {
        get { return true; }
    }
}
fARcRY