asyncFileUpload how to get new File Name - c#

asyncFileUpload how to get a new File Name after upload complete to save new name in database
asp.net c#
I have a form to fill in personal data and Upload own image
Asyncfileupload used to select the picture and in the void AsyncFileUpload_UploadedComplete
Save the file in new name
Now when the user ends of the mobilization of all form and presses the save button to save data , i save every thing in database I can not get the new name to save it.

it is a property in the control. In you event handler check the FileName property. The following code assumes your are checking the file size to to intercept any oversized files.
protected void AsyncFileUpload_UploadedComplete(object sender, AsyncFileUploadEventArgs e)
{
AsyncFileUpload upload = sender as AsyncFileUpload;
if ((upload.PostedFile.ContentLength / 1000) <= 500)
{
string filename = upload.FileName;
// save to the database here
}
}
Hope this helps :-)
Mike

Related

Read Multiple Textfile upon Button Click and Display Content

I initially have a Fileupload tool to upload a textfile, manipulate its content and display into a Listbox or Textbox. The limitation however is Fileupload only supports single uploading, at least to the version of .Net Framework I am using.
What I intend to do is just use a button control and remove the Fileupload. Upon Button click I need to read the textfiles inside a designated folder path and display first the contents inside a multiple lined textbox. (not just the file name) This is my intially written codes, and it is not working.
protected void btnGetFiles_Click(object sender, EventArgs e)
{
string content = string.Empty;
DirectoryInfo dinfo = new DirectoryInfo(#"C:\samplePath");
FileInfo[] Files = dinfo.GetFiles("*.txt");
foreach (FileInfo file in Files)
{
//ListBox1.Items.Add(file.Name);
content += content;
}
txtContent.Text = content;
}
Since your's is web based application you can't access physical paths like c:\\.. you should use Server.MapPath anyway(As per the comment, you don't need to get the file with Server.MapPath). Then for getting the content you can try something like the following:
protected void btnGetFiles_Click(object sender, EventArgs e)
{
try
{
StringBuilder content = new StringBuilder();
if (Directory.Exists(#"C:\samplePath"))
{
// Execute this if the directory exists
foreach (string file in Directory.GetFiles(#"C:\samplePath","*.txt"))
{
// Iterates through the files of type txt in the directories
content.Append(File.ReadAllText(file)); // gives you the conent
}
txtContent.Text = content.ToString();
}
}
catch
{
txtContent.Text = "Something went wrong";
}
}
you wrote content += content;, that is the problem. change it to content += file.Name;, it will work.

How to show up an image when an option is selected in dropdownlist in asp.net?

I am working on a website i.e a tours and travel website in which I have been stuck at a place where I need to show up an image when a option is selected in drop down list and the image is retrieved from database and the image is stored in binary data and also the data value of drop down list is retrieved from database so I wanted to display the image as soon as the option is selected in drop down list .Below is the database I have created:
Form Image:
Assuming that u are using webforms,
Follow the Steps :
Add Eventlistener onDropdownSelectionChanged
In event Listener -
Fetch Using id from Database
Get the ImageButton / Tag
Convert to image and save in temp path
Display By Setting ImageUrl Property
if (DropDownList1.SelectedValue == "value 1")
{
Image1.ImageUrl = "images/image-1.png";
}
else if (DropDownList1.SelectedValue == "value 2")
{
Image1.ImageUrl = "images/image-2.png";
}
...
...
...
else
{
Image1.ImageUrl = "images/image-n.png";
}

Scrolling list with multiple objects

I have been making a form to let the user save his progress. There are 6 virtual slots which contain different save files read from a folder. I want to have the same set up just with a scrollbar to let user scroll through save files, in case he has more than 6 made.
The set up is: picturebox which loads save file when clicked, label for file name and for file date, picturebox to delete the save file, and a panel underneath to save file when the slot is clicked.
Below is the code I use to load in 6 save files. (I will get the date by reading the save file start as it contains the date, but I have not done that part yet).
private void loadsavestoscreen()
{
string filename;
string extension;
string locpath = #"C:\test";
String[] allfiles = System.IO.Directory.GetFiles(locpath, "*.*", System.IO.SearchOption.TopDirectoryOnly);
int counter = 0;
foreach (String file in allfiles)
{
if (counter == 6 || counter == allfiles.Length - 1)
{ labelcheck(); break; }
if ((extension = Path.GetExtension(file)) == ".dat")
{
filename = Path.GetFileNameWithoutExtension(file);
//Console.WriteLine(filename);
changelbl(counter, filename);
counter++;
}
}
}
'labelcheck' checks if the text is correct, if not it hides the label.
'lblchange' changes the name of the label on the correct slot.
My question is: How would I implement a scrollbar to allow the user to scroll through more save files when there is more than 6?
Here's a snippet of the form:
I am slightly new to programming so my apologies if I've made some obvious errors. Thanks for any help.
Without a list object or a container this is not very easy to solve.
I suggest you to use a DataGridView or a ListView object. You can easily add your file entries as objects to these lists. They have an option Scrollable, which you can set true or false.
I would also create a class for that save file entries (storing label/image position and contents) and add them to your DataGridView or ListView.
If you want to know how to add images to those controls:
How do add image to System.Windows.Forms.ListBox?

Couldn't assign value in FileUpload tool

I grabbed the value from database and I am trying to assign those values in Edit Form. But the only problem is with the FileUpload. It don't take the value. Can anyone suggest me what I'm missing here
private void EditForDataByID(int TitleId)
{
ReadmoreController objFormController = new ReadmoreController();
ReadMoreInfo objInfo = objFormController.GetListObjectOfAllArticle(TitleId);
if (objInfo != null)
{
TextTitle.Text = objInfo.Title;
txtSummary.Text = objInfo.Summary;
TextDate.Text = objInfo.Date.ToString();
//FileUpload1.FileName=objInfo.Image; I even tried this but it doesn't work
FileUpload1 = objInfo.Image;
Session["TitleId"] = TitleId;
ListDiv.Visible = false;
form.Visible = true;
BindGrid();
}
}
For client security reason you can not assign value to FileUploadControl as it could cause the uploading of unwanted files from client machine. So let the use pick the file to upload.
If it is allowed then one can steel the important files from client machine like c:\PersonalPasswords
Edit Based on comments
If you need to ensure that user has selected an Image and does not need to change it then you can use a image control and assign image to it. Use the same image control to find if the image is selected or not.

C# List text files in a listbox and let user open them in a textbox

I'am pretty new to C# and I want a listbox with all text files in a folder and if the user dubbleclicks on the listed file the file will display in a textbox.
I don't want to use the openFileDialog function because the textfiles are on a webserver which I access using username:password#server.com/folder.
Kinda like a texteditor limited to edit only files in 1 folder :)
If this is possible using openFileDialog please tell me how.
I hope you can understand what I want to do.
Greetings,
From what I understand you are after, you want to iterate through the files in a specific directory and then allow them to be edited once opened with a double click in a listbox.
This can be done using the var Files = Directory.GetFiles("path", ".txt");
Files would be a string[] of the file names.
Then filling the Listbox with the files something like this:
ListBox lbx = new ListBox();
lbx.Size = new System.Drawing.Size(X,Y); //Set to desired Size.
lbx.Location = new System.Drawing.Point(X,Y); //Set to desired Location.
this.Controls.Add(listBox1); //Add to the window control list.
lbx.DoubleClick += OpenFileandBeginEditingDelegate;
lbx.BeginUpdate();
for(int i = 0; i < numfiles; i++)
lbx.Items.Add(Files[i]);
lbx.EndUpdate();
Now your event delegate should look something like this:
OpenFileandBeginEditingDelegate(object sender, EventArgs e)
{
string file = lbx.SelectedItem.ToString();
FileStream fs = new FileStream(Path + file, FileMode.Open);
//Now add this to the textbox
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b,0,b.Length) > 0)
{
tbx.Text += temp.GetString(b);//tbx being the textbox you want to use as the editor.
}
}
Now to add an event handler through the VS window editor click on the control in question and go to the properties pane for that control. You will need to then switch to the events pane and scroll untill you find the DoubleClick event if you use that the designer should auto-insert a valid delegate signature and allow you to write the logic for the event.

Categories

Resources