return XML file to user - asp.net

I am creating a XML file which I want to return to the user, when the user goes to a specified location. I have thought of something like:
return new FileStream("questions.xml",FileMode.Open);
Is this the proper code, or how would you return a file?

protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
var file = Server.MapPath("~/questions.xml");
Response.WriteFile(file);
Response.ContentType = "text/xml";
Response.AddHeader("Content-Disposition", "attachment; filename=questions.xml");
}

Related

Download multiple files from gridview from download button

I am saving the path of the file in database and file is in server folder and the gridview control is used to show the files from the database. i need to download the selective files from the gridview using download button Click event.
Here is my code:
protected void btn_download_Click(object sender, EventArgs e)
{
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
zip.AddDirectoryByName("records");
foreach (GridViewRow row in GridView1.Rows)
{
if ((row.FindControl("chkSelectoptn") as CheckBox).Checked)
{
string filePath = (row.FindControl("lblFilePath") as Label).Text;
zip.AddFile(filePath, "records");
}
}
Response.Clear();
Response.BufferOutput = true;
string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
zip.Save(Response.OutputStream);
Response.End();
}
}
I am using a label lblFilePath to give the path of the file name which we had store in the database.
But "FileNotFoundException" occurs.
Please suggest.

Upload my files into folder using asp.net

I want to upload my files into folder like
FileUpload1.SaveAs(Server.MapPath("~/admin_file/") + FileUpload1.FileName);
here my folder name admin_file.
protected void BindGrid() {
string[] filePaths = Directory.GetFiles(Server.MapPath("~/admin_file/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileName(filePath), filePath));
}
GridView1.DataSource = files;
GridView1.DataBind();
}
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
FileUpload1.SaveAs(Server.MapPath("~/admin_file/") + FileUpload1.FileName);
BindGrid();
}
else
{
//Response.Write("Please select file to upload");
string message = "alert('Please select file to upload!')";
ScriptManager.RegisterClientScriptBlock((sender as Control), this.GetType(), "alert", message, true);
}
}
protected void DownloadFile(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.WriteFile(filePath);
Response.End();
}
protected void DeleteFile(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
File.Delete(filePath);
string message = "alert('Deleted Successfully!')";
ScriptManager.RegisterClientScriptBlock((sender as Control), this.GetType(), "alert", message, true);
BindGrid();
}
This program execute successfully in local system. When I publish this program to GODADDY not support so anybody tell how to give this path
otherwise send mail pramadivi#gmail.com
Read this troubleshooting article and follow according to it.
its better practice to use Path.Combine when concatenating folder+file name
FileUpload1.SaveAs(System.IO.Path.Combine(pathToFolder, fileName));

How to let user select the location for downloading a file in asp.net?? To be precise a open file dialog equivalent in asp(Not fileUpload)

I am downloading a pdf file in my project. I want to let the user decide where to download the File. Quick help is helpful.
Thanks
try this:
It will display a dialog box in the browser and the user will select on where to save the file
protected void DownloadFile_Click(object sender, EventArgs e)
{
String Filepath;
System.IO.FileInfo file = new System.IO.FileInfo(Filepath); // full file path on disk
Response.ClearContent(); // Clear previous content
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/pdf";
Response.TransmitFile(file.FullName);
Response.End();
}
You haven't specified how your application is serving this PDF file, but assuming that you have some WebForm which is streaming it to the Response, you should set the Content-Disposition header as attachment in order to force the Save As dialog in the browser.
For example:
protected void Download(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=example.pdf");
Response.WriteFile(#"c:\work\example.pdf");
}

How to browse & display excel sheet in web browser using asp.net?

i want to browse the excel input file & display it on web browser & it must be editable on web browser,
i am using C# asp.net for coding. Any help would be gr8. Thnks in advance.
I think the options you have are HTML tables or Google Spreadsheets / Microsoft Excel Online.
For more info:
http://www.labnol.org/software/embed-tables-spreadsheet-data-in-websites/7435/
In the webform:
Code behind:
protected void UploadFile(object sender, EventArgs e)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName);
Response.Redirect(Request.Url.AbsoluteUri);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileName(filePath), filePath));
Fileddl.Items.Add(new ListItem(Path.GetFileName(filePath), Path.GetFileName(filePath)));
}
}
}
protected void OpenFile(object sender, EventArgs e)
{
string filePath = Fileddl.SelectedItem.Value;
Myframe.Visible = true;
Myframe.Attributes.Add("src", "https://view.officeapps.live.com/op/embed.aspx?src=https://www.yoursite.com/Uploads/" + Path.GetFileName(filePath));
}
protected void DownloadFile(object sender, EventArgs e)
{
string filePath = Fileddl.SelectedItem.Value;
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=https://www.yoursite.com/Uploads/" + Path.GetFileName(filePath));
Response.WriteFile(filePath);
Response.End();
}
protected void DeleteFile(object sender, EventArgs e)
{
string filePath = Fileddl.SelectedItem.Value;
File.Delete(filePath);
Response.Redirect(Request.Url.AbsoluteUri);
}

Download a string as a file in ASP.NET

The following method, based on code in this question, shows a file download dialog box in the browser, but then the download never starts (it stays at 0%):
protected void lnkExport_Click(object sender, EventArgs e) {
var bytes = Encoding.ASCII.GetBytes(SelectRecords()); //Data to be downloaded
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=\"test.xls\"");
using (var stream = new MemoryStream(bytes)) {
Response.AddHeader("Content-Length", stream.Length.ToString());
stream.WriteTo(Response.OutputStream);
}
}
Any idea what's up?
Your code worked fine for me but you may want to try adding this as the last line of your click handler:
Response.End();

Resources