Downloading csv files with zip? - c#

I have this codes, the problem is, whenever I press the download button, it gives an error indicating Directory Not Found. I have already an upload function with the following fileUpload.PostedFile.SaveAs(Server.MapPath("~/Upload")); Below is my codes:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var files = Directory.GetFiles(#"~/Upload");
gvFiles.DataSource = from f in files
select new
{
FileName = Path.GetFileName(f)
};
gvFiles.DataBind();
}
}
protected void btnDownload_Click(object sender, EventArgs e)
{
string fileName = string.Empty;
string filepath = Request.MapPath("~/Upload");
string downloadFileName = "Attendance.zip";
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "filename=" + downloadFileName);
using (ZipFile zip = new ZipFile())
{
foreach (GridView row in gvFiles.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("chkSelect");
if (cb != null && cb.Checked)
{
fileName = (row.FindControl("lblFileName") as Label).Text;
zip.AddFile(Server.MapPath(Path.Combine(filepath, fileName)), "");
}
}
zip.Save(Response.OutputStream);
}
}
Can anyone help me with this please? When I use Directory.GetFiles(#"~/Upload"), I get the mentioned error

Directory.GetFiles expects an existing local path, die ~ Syntax is not possible here. Use MapPath before:
var files = Directory.GetFiles(Request.MapPath("~/Upload"));

Related

C# FilePath Problems WinForms

I am trying to output the file address of an item selected in a combobox. But i keep getting the Directory address of the project and not the item itself. Please help. Here is my Code:
private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
if (availableSoftDropBox.SelectedItem.Equals("Choose Your Own..."))
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
txtFlashFile.Text = openFileDialog1.FileName;
}
else
{
string fileName;
fileName = Path.GetFullPath((string)availableSoftDropBox.SelectedItem);
string fullPath = #"C:\Program Files (x86)\yeet-n . master\yeet-
master\src\yeet\System\Products\" + (fileName);
txtFlashFile.Text = fullPath;
I'm not quite sure what you want to achieve, but using FileInfo instead of Path.GetFullPath might help.
private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
const string fullPath = #"C:\Program Files (x86)\yeet-n . master\yeet-
master\src\yeet\System\Products\";
string selection = (string)availableSoftDropBox.SelectedItem;
var fileInfo = new FileInfo(fullPath + selection);
string text = fileInfo.FullName;
if (selection.Equals("Choose Your Own..."))
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
text = openFileDialog1.FileName;
}
}
txtFlashFile.Text = text;
}

Uploaded images are not found in folder asp.net

I have a upload function where it displays the uploaded image in a grdiview after the upload click.
Here is the code:
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)
{
string fileName = Path.GetFileName(filePath);
files.Add(new ListItem(fileName, "~/Uploads/" + fileName));
}
GridView1.DataSource = files;
GridView1.DataBind();
}
}
protected void Upload(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName);
Response.Redirect(Request.Url.AbsoluteUri);
}
}
}
}
This works fine. It uploads and shows in the gridview. The problem I have is that the pictures are not directed to the path. Its not in the uploads folder.
Any tricks on this?
UPDATE
Show all in solution explorer and i got this:
On possible solution could be, when saving/uploading files, use Path.Combine
FileUpload1.PostedFile.SaveAs( Path.Combine(Server.MapPath("~/Uploads/"),fileName))
and similarly for:
files.Add(new ListItem(fileName,Path.Combine(Server.MapPath("~/Uploads/"),fileName)));
Mostly i used following approach.
you can get help from following example code...
string fnam, newname,ext, serpath,dbpath="", fid;
ext = System.IO.Path.GetExtension(File_Upload.PostedFile.FileName);
fnam = File_Upload.PostedFile.FileName;
fid = Guid.NewGuid().ToString();
newname = fid + ext;
serpath = Path.Combine(Server.MapPath("uploads\\"), newname);
dbpath = "~\\uploads\\" + newname;
File_Upload.PostedFile.SaveAs(serpath);

How search a file by its name and open it? [duplicate]

This question already has an answer here:
How to open a pdf file in a WebForm application by search?
(1 answer)
Closed 7 years ago.
Currently i'm doing a web application project in ASP.net C#.
Here i have a problem to search a file by its name. Below code is shows were i did, but the problem is, it does not shows the file according to the search name, since it show all file name in directories.
Another problem is, i don't how to open the search files. Can any one help me?
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "")
{
string[] pdffiles = Directory.GetFiles(#
"\\192.168.5.10\\fbar\\REPORT\\CLOTHO\\H2\\REPORT\\", "*.pdf", SearchOption.AllDirectories);
string search = TextBox1.Text;
ListBox1.Items.Clear();
foreach(string file in pdffiles)
{
ListBox1.Items.Add(Path.GetFileName(file));
}
TextBox1.Focus();
} else
{
Response.Write("<script>alert('For this Wafer ID Report is Not Generated');</script>");
}
}
First you have to use your search variable to filter out intended files
protected void Button1_Click(object sender, EventArgs e)
{
string search = TextBox1.Text;
if (TextBox1.Text != "")
{
string[] pdffiles = Directory.GetFiles(#"\\192.168.5.10\\fbar\\REPORT\\CLOTHO\\H2\\REPORT\\", string.Format("*{0}*.pdf",search), SearchOption.AllDirectories);
ListBox1.Items.Clear();
foreach (string file in pdffiles)
{
ListBox1.Items.Add(Path.GetFileName(file));
}
TextBox1.Focus();
}
else
{
Response.Write("<script>alert('For this Wafer ID Report is Not Generated');</script>");
}
}
Now to open selected file.
protectecd void ListBox1_SelectedIndexChanged(object sender,EventArgs e)
{
string fileName= ListBox1.SelectedItem.ToString();
Response.ContentType = "Application/pdf";
Response.AppendHeader("Content-Disposition",string.Format("attachment; filename={0}",filename));
Response.TransmitFile(fileName);
Response.End();
}
you need to use your string search to check if file matches it
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "")
{
File[] pdffiles = Directory.GetFiles(#"\\192.168.5.10\fbar\REPORT\CLOTHO\H2\REPORT\", "*.pdf", SearchOption.AllDirectories);
string search = TextBox1.Text;
ListBox1.Items.Clear();
foreach (var file in pdffiles)
{
if(file.Name==search)
{
ListBox1.Items.Add(Path.GetFileName(file));
}
}
TextBox1.Focus();
}
else
{
Response.Write("<script>alert('For this Wafer ID Report is Not Generated');</script>");
}
}
Also notice you have written the path in GetFiles function
I think the path should be #"\\192.168.5.10\fbar\REPORT\CLOTHO\H2\REPORT\". Also, Directory.EnumerateFiles might be more efficient.
Here's how I would search for any files that CONTAIN the searchName
using System.Linq;
string reportDirectoryName = "..."; // fill in with full path
string searchName = TextBox1.Text;
if (string.IsNullOrWhitespace(searchName))
return ...;
var files = Directory.EnumerateFiles(reportDirectoryName, "*.pdf", SearchOption.AllDirectories);
.Select(n => Path.GetFileName(n))
.Where(n => n.Contains(searchName);
ListBox1.Items.Clear();
ListBox1.Items.Add(files);

How can I update image in listview in gridview format?

how can i update image in listview in gridview formate using folder path in asp.net in c#?
protected void UpdateButton_Click(object sender, EventArgs e)
{
TextBox ApplicantIdTextBox = (TextBox)RadListView8.FindControl("ApplicantIdTextBox");
FileUpload photoTextBox = (FileUpload)RadListView8.FindControl("photoTextBox");
string fileName1 = Path.GetExtension(ApplicantIdTextBox + photoTextBox.FileName);
string fileSavePath = Server.MapPath("ImageStorage/" + fileName1);
tblPersonalInfo pi = new tblPersonalInfo();
pi.photo = fileName1;
photoTextBox.SaveAs(fileSavePath);
dbcontext.AddTotblPersonalInfoes(pi);
dbcontext.SaveChanges();
}
But it show me error...What can i do?
Server Error in '/HrPayRoll' Application.
Object reference not set to an instance of an object.
I'd rewrite it something like this and then run it in debug mode and see what line the error is on. You were trying to use applicantIdTextBox as a string, although I would have thought that would give a different error:
protected void UpdateButton_Click(object sender, EventArgs e)
{
TextBox applicantIdTextBox = RadListView8.FindControl("ApplicantIdTextBox") as TextBox;
FileUpload photoTextBox = RadListView8.FindControl("photoTextBox") as FileUpload;
if ((applicantIdTextBox != null) && (photoTextBox != null))
{
string fileName = Path.GetExtension(applicantIdTextBox.Text + photoTextBox.FileName);
string fileSavePath = Server.MapPath("ImageStorage/" + fileName);
tblPersonalInfo personalInfo = new tblPersonalInfo();
personalInfo.photo = fileName;
photoTextBox.SaveAs(fileSavePath);
dbcontext.AddTotblPersonalInfoes(personalInfo);
dbcontext.SaveChanges();
}
}

How do I upload to dropbox using dropnet?

I am using DropNet. I have problem with upload file into DropBox.
I am sure the connection with dropbox in fine. when I changed the method of upload to create file and delete file method that works fine.
I really can not see any problem that why is not uploading? I use exactly same API as DropNet.
protected void Btn_upload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
if (Session["DropNetUserLogin"] != null)
{
try
{
_client.UseSandbox = true;
_client.UploadFile("/", FileUpload1.FileName, FileUpload1.FileBytes);
}
catch (Exception ex)
{
litOutput.Text = "Error in upload user login in session " + ex.Message;
}
}
else
{
litOutput.Text = "Session expired...";
}
}
else
{
litOutput.Text = "You did not specify a file to upload.";
}
}
}
Here is code which worked for me, hoping that this may help you:
private void button1_Click(object sender, RoutedEventArgs e)
{
var dlg = new OpenFileDialog();
dlg.Filter = "Text documents (.txt)|*.txt";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
string filename = dlg.FileName;
FileNameTextBox.Text = filename;
}
var x = #"/" + Path.GetFileName(FileNameTextBox.Text);
_client.UploadFile("/", Path.GetFileName(FileNameTextBox.Text), File.ReadAllBytes(#"" + FileNameTextBox.Text));
}

Categories