Getting the path of a file using fileupload control - c#

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

Related

How do you display an image on webpage using file path stored on a database?

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);

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.

asyncFileUpload how to get new File Name

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

CreateDirectory with AjaxFileUpload into a specific folder

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.

Ajax ReordeList and asp:FileUpload Problem

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.

Categories

Resources