I am trying to create a folder then upload documents to that same folder with AjaxFileUpload function, and I need the folder path to include the value of a text box in my form; however, I cannot seem to find any good resources to show me how to do so, so any help would be appreciated. Here is my code:
aspx page (the below is within an update panel):
<asp:AjaxFileUpload ID="CertificateUpload" ThrobberID="myThrobber" runat="server" MaximumNumberOfFiles="10" Width="600px" OnUploadStart="CreateFolder_Click" OnUploadComplete="File_Upload" />
apsx.cs page:
protected void CreateFolder_Click(object sender, EventArgs e)
{
string folderName = #"P:\Training Records\Training Detail Records\Individual Records";
string pathString = Path.Combine(folderName, firstnametier1.Text + " " + lastnametier1.Text);
if (!Directory.Exists(pathString))
{
Directory.CreateDirectory(pathString);
}
}
protected void File_Upload(object sender, AjaxFileUploadEventArgs e)
{
string filename = e.FileName;
string FinalFolder = "~/Training Detail Records/Individual Records/";
string strDestPath = Server.MapPath(#FinalFolder);
CertificateUpload.SaveAs(#strDestPath + filename);
}
}
I am trying to put the these two together so the folder gets created if necessary when I hit the upload button and I would like to add an extra text value to the final folder path like this:
string FinalFolder = "~/Training Detail Records/Individual Records/" + Textbox.Text + "/";
But when I try this it does not work. Again, any help will be welcome.
Thanks
I figured it out:
Here is the code for the aspx.cs page:
first I added the following to the SelectedIndexChanged(object sender, EventArgs e) section:
string fullname = firstnametier1.Text + " " + lastnametier1.Text;
Session["fname"] = fullname;
That helped store the value of the two text fields in the session so I could use it in the ajaxfileupload function.
Second I adjusted my other piece of code like so:
protected void File_Upload(object sender, AjaxFileUploadEventArgs e)
{
string folderName = "P:/Training Records/Training Detail Records/Individual Records/" + Session["fname"].ToString() + "/";
if (!Directory.Exists(folderName))
{
Directory.CreateDirectory(Server.MapPath(folderName));
}
string filename = e.FileName;
string strDestPath = Server.MapPath(#folderName);
CertificateUpload.SaveAs(#strDestPath + filename);
}
And now it works! I am creating a folder with the name I want and adding the data to it.
I am not a professional programmer so if you see any issues with this code or can make it better, please do so. I hope this helps other people.
Related
I've got a WCF Service Library with all the SQL statements on. A Windows Forms Admin app is updating the Service and an ASP.Net Web Forms app is consuming the Services.
File paths are stored as a strings in the database to change images in the Picture Box on the WinForms app.
I thought I could just pass the filePath into a string variable on the ASP.Net end and use that for Image.Url. Its not working.
Whats the simplest way of doing this? Security is not an issue, this is just for an assignment.
When the user changes the Cottage ID the corresponding image should change on the web site.
C#
protected void BtnID_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(TextBoxID.Text);
string fileName = ws.GetImage1FileName(id);
Image1.ImageUrl = fileName;
}
Html
<td class="Column3" colspan="1">
<asp:Image class="Image" ID="Image1" runat="server"/>
</td>
TIA!
assume your data out put as
string fileName = "123.png";
protected void BtnID_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(TextBoxID.Text);
string fileName = ws.GetImage1FileName(id);
Image1.ImageUrl = Server.MapPath("~/images") + "/" + fileName;
}
In winforms you can try with Bitmap
Image1.Image = new Bitmap(YourFileName);
I'm doing a C# 3-tier project about pets
On load of the first webform I make a query to the database and get the data of all pets (id, name, photo) as a List to be shown as cards with images in HTML, I'm using Materialize by the way, I leave this link as example http://materializecss.com/cards.html
My code behind (Pets.aspx.cs) is this
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
NegPet negPet = new NegPet();
StringBuilder sbHtml = new StringBuilder();
List<EntPet> listEntPet = negPet.getAllPet(5); //5 is the user code (get all pets from user 5)
if (listEntPet.Count > 0)
{
foreach (EntPet entPet in listEntPet)
{
sbHtml.Append("<div class=\"col s6 m4\">");
sbHtml.Append("<div class=\"card\">");
sbHtml.Append("<div class=\"card-image\">");
sbHtml.Append("<img src=\"http://www.dogzone.com/images/breeds/beagle.jpg\" alt=\"\" class=\"circle responsive-img\" />");
sbHtml.Append("<asp:LinkButton id=\"lb_"+ entPet.Id_pet + "\" runat=\"server\" CommandArgument='<%# Eval(\""+ entPet.Id_pet + "\") %>')");
sbHtml.Append("\" click=\"updatePet_Click\" class=\"btn-floating halfway-fab waves-effect waves-light red\"><i class=\"material-icons\">edit</i></a>");
sbHtml.Append("</div>");
sbHtml.Append("<div class=\"card-content\">");
sbHtml.Append("<span class=\"card-title\">");
sbHtml.Append(entPet.Name_pet);
sbHtml.Append("</span>");
sbHtml.Append("</div>");
sbHtml.Append("</div>");
sbHtml.Append("</div>");
}
} else
{
sbHtml.Append("<h2>No pets found</h2>");
}
galPet.Controls.Add(new Literal { Text = sbHtml.ToString() });
}
}
Where galPet is a
<asp:PlaceHolder ID="galPet" runat="server" />
This code returns me all the "Id" and "Name" of "Pets" and sets it in the HTML design that I want, similar to a gallery. The problem comes when I try to get to the event onClick="updatePet_Click" apparently it never reaches it's method behind
public void updatePet_Click(Object sender, EventArgs e)
{
LinkButton btn = (LinkButton)(sender);
string yourValue = btn.CommandArgument.Substring(3);
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + yourValue + "');", true);
}
What I'm trying to do with this code is retrieve the ID from the clicked asp:LinkButton so I can use it in code behind
I also tried changing the event to "OnClick" but I got this error when tried to click it
Pets.aspx:24 Uncaught ReferenceError: updatePet_Click is not defined
at HTMLUnknownElement.onclick (Pet.aspx:24)
I would like to know how to retrieve the ID to work it in the code behind or if there is another way to pass my list to the HTML design where I can get the clicked ID easier. Thanks
I've been having a problem with the following code:
namespace Viewer
{
public partial class Form1 : Form
{
int count = 0;
LinkLabel[] linkLabel = new LinkLabel[200];
string filename;
string extension;
string filepath;
private void btnLoad_Click(object sender, EventArgs e)
{
// Creates a Directory for the Movies Folder
DirectoryInfo myDirectory = new DirectoryInfo(#"C:\Users\User\Movies");
// Creates a list of "File info" objects
List<FileInfo> ls = new List<FileInfo>();
// Adds filetypes to the list
ls.AddRange(myDirectory.GetFiles("*.mp4"));
ls.AddRange(myDirectory.GetFiles("*.avi"));
// Orders the list by Name
List<FileInfo> orderedList = ls.OrderBy(x => x.Name).ToList();
// Loop through file list to act on each item
foreach (FileInfo filFile in orderedList)
{
// Creates a new link label
linkLabel[count] = new LinkLabel();
// Alters name info for display and file calling
filepath = filFile.FullName;
extension = filFile.Extension;
filename = filFile.Name.Remove(filFile.Name.Length - extension.Length);
// Write to the textbox for functional display
textBox1.AppendText(filename + "\r\n");
// Alters link label settings
linkLabel[count].Text = filename;
linkLabel[count].Links.Add(0, linkLabel[count].Text.ToString().Length, filepath);
linkLabel[count].LinkClicked += new LinkLabelLinkClickedEventHandler(LinkedLabelClicked);
// Adds link label to table display
tblDisplay.Controls.Add(linkLabel[count]);
// Indexes count up for arrays
count = count + 1;
}
}
private void LinkedLabelClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start(filepath);
}
}
}
My goal is to generate a table of links to all of the media files that I add at launch, and have the links open the files in their respective players.
As of right now, it generates all of the links properly, but whenever I click on any of them, it launches the last item in the list.
For example, if the list contains "300", "Gladiator", and "Top Gun", no matter which link I click, it opens "Top Gun".
I assume that this has to do with it calling the variable "filepath" in the click event, which is left in it's final state. However, I'm not exactly clear on how to create a static link value or action on each individual link, as all of the answers I've researched are in regards to single linklabel situations, not dynamic set-ups.
Any help/advice would be appreciated!
Try as below:
In foreach loop add one line more like:
linkLabel[count].Tag = filepath;
then in click event get this path as blow,
private void LinkedLabelClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
string filepath = ((LinkLabel)sender).Tag.tostring();
System.Diagnostics.Process.Start(filepath);
}
I am using a fileupload control to display the contents of a text file in a textbox..if i use this
<asp:FileUpload ID="txtBoxInput" runat="server" Text="Browse" />
string FilePath = txtBoxInput.PostedFile.FileName;
it will get only the file name like bala.txt.i need like this D:\New Folder\bala.txt
Instead of fileupload control i have used textbox to get the path like this D:\New Folder\bala.txt
<asp:TextBox ID="txtBoxInput" runat="server" Width="451px"></asp:TextBox>
string FilePath = txtBoxInput.Text;
But i need browse button instead of textbox to get the path...Any Suggestion??
EDIT:My button click event
protected void buttonDisplay_Click(object sender, EventArgs e)
{
string FilePath = txtBoxInput.PostedFile.FileName;
if (File.Exists(FilePath))
{
StreamReader testTxt = new StreamReader(FilePath);
string allRead = testTxt.ReadToEnd();
testTxt.Close();
}
}
You can get the file name and path from FileUpload control only when you are in debug mode but when you deployed your app. in server then you cant because that is your client address which you are trying to access by server side code.
protected void Button1_Click(object sender, EventArgs e)
{
string filePath,fileName;
if (FileUpload1.PostedFile != null)
{
filePath = FileUpload1.PostedFile.FileName; // file name with path.
fileName = FileUpload1.FileName;// Only file name.
}
}
If you really want to change properties or rename client file then you can save file on server on temp folder then you can do all thing which you want.
http://forums.asp.net/t/1077850.aspx
I have a ReorderList which is working fine, inside the InsertItemTemplate, I've added a asp:Fileupload to add images to the list and database. All of these controls are inside a DIV.
How could I reach to this (asp:FileUpload) in C# to check whether it has a file or not,
this is the C# part of the code:
///////////////////////////////////////////////////////////////////////////////////////////////
protected void btnInsert_Click(object sender, EventArgs e)
{
string sFilename = Guid.NewGuid().ToString();
FileUpload filePhoto = (FileUpload)div1.FindControl("filePhoto");
if (filePhoto.HasFile)
{
string sPath = "";
string sFile = filePhoto.FileName.ToString();
sPath = Server.MapPath("Images");
filePhoto.SaveAs(sPath + "\\" + sFile);
//to fill the Notice image by code behine
ObjectDataSource1.InsertParameters["theImage"].DefaultValue = "Images\\" + sFile;
}
else
{
//to fill the Notice image by code behine
ObjectDataSource1.InsertParameters["theImage"].DefaultValue = "Images\\" + "NoImage.jpg";
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
any ideas?
Thanks in advance
Actually ReorderList is an ajax control and you cannot use normal Asp:Fileuploader in ajax control. You have to use the asyncfileuploader control of ajax control toolkit in order to work in ajax application.