Sakthivel
Sakthivel

Reputation: 1941

How to upload and download file from a server using C#

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?

Upvotes: 1

Views: 4351

Answers (4)

user2354473
user2354473

Reputation: 1

For uploading file you can use asp:FileUpload control and simple use SaveAs method. But be aware that if you use it in combination with partial update object (asp:UpdatePanel) that you'll allso have to set triggers. If you don't use triggers asp:FileUpload control can be resetted...

Upvotes: 0

fARcRY
fARcRY

Reputation: 2358

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

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 837936

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

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

Upvotes: 0

Adriaan Stander
Adriaan Stander

Reputation: 166326

You can stream the document back using Response stream.

This might get you going.

Response

Request, Response Objects

Upvotes: 0

Related Questions