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);
Related
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;
}
Hy everyone,
I would like to copy multiple selected files with openfiledialog to a folder which is defined as #"C:\TestFolder\"+ textBox1.Text. My problem is that somehow the program writes the textBox content in the file name too.
Please find below my code:
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog od = new OpenFileDialog();
od.Filter = "All files (*.*)|*.*";
od.Multiselect = true;
if (od.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string targetPath = #"C:\TestFolder\"+ textBox1.Text;
string path = System.IO.Path.Combine(targetPath, textBox1.Text);
if (!System.IO.Directory.Exists(targetPath)
{
System.IO.Directory.CreateDirectory(targetPath);
}
foreach (string fileName in od.FileNames)
{
System.IO.File.Copy(fileName, path + System.IO.Path.GetFileName(fileName));
}
}
}
Any input would be appreciated!
Try this one:
string Main_dir = #"C:\TestFolder\";
string Sub_dir = textBox1.Text + #"\";
string targetPath = System.IO.Path.Combine(Main_dir, Sub_dir);
{
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
foreach (string fileName in od.FileNames)
System.IO.File.Copy(fileName, targetPath + System.IO.Path.GetFileName(fileName), true);
}
Backslash is missing
#"\"
These things are equivalent.
string targetPath = #"C:\TestFolder\"+ textBox1.Text;
string path = System.IO.Path.Combine(targetPath, textBox1.Text);
I would drop the first one for the Path.Combine call as it is portable and robust when it comes to the separators.
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);
private void btn_add_image_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "Choose a file";
openFileDialog1.InitialDirectory = "C:\\";
openFileDialog1.Filter = " JPEG Files (*.jpg;*.jpeg;*.jpe;*.jfif)|*.jpg|All Files (*.*)|*.*";
openFileDialog1.ShowDialog();
string file_name = openFileDialog1.FileName;
string filename2 = openFileDialog1.SafeFileName;
pictureBox1.Image = Image.FromFile(file_name);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
string[] extension = getExtension("images\\" + userid);
if (File.Exists("images\\" + userid + extension[0]))
{
File.Delete("resimler\\" + userid + extension[0]);
}
}
catch (Exception)
{
MessageBox.Show("İmage cannot find");
}
I want to Change File name and save , so i wrote this code if file exists , than delete file and save the choosen with userid name but i cant do change name and save file
if (File.Exists(#"\path\to\source"))
{
File.Move(#"\path\to\source",#"\path\to\destination")
}
I think both your problems can me handled with this bit of code.
System.IO.File.Move("old_file_name_path", "new_file_name_path");
This moves the file to a new filename. Take a look here: File.Move
But, I really don't get what you are asking here:
i wrote this code if file exists , than delete file and save the
choosen with userid name but i cant do change name and save file
Can you be more specific?
Thanks All
private void btn_save_Click(object sender, EventArgs e)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
string source = openFileDialog1.FileName;
string[] extension = getExtension(source);
string destination = "images\\" + userid + extension[0];
System.IO.File.Move(source, destination);
pictureBox1.Image = Image.FromFile("images\\" + userid + extension[0]);
}
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"));