I have a simple request. I have this:
So, in my .aspx :
<asp:FileUpload ID="myFileUpload" runat="server" onclick="LoadFile_Click" />
After the pression of the "Load file" button, I want that the page will be filled with the data of the document choosen. Like this:
In my .aspx.cs
protected void LoadFile_Click(Object sender, EventArgs e)
{
string address = // a way to get the path of the file. How?
BindExcelToPage(address); // this method fills the web page with the data taken from the file uploaded;
}
The informative phrase "No file selected" (or whatever) must be invisible. I want just the Load button.
I don't know how to implement the starts of the event for the loading of the page.
After you BindExcel file with page, hide your fileupload control and replace with it label(if you want use label and button) and set the filename in label. This way it looks like file is there. FileUpload control's filename is readonly, so you can't assign it.
Related
I'm using the ASP.NET FileUpload control.
To upload a file, a user must:
Browse to select a file from his/her device,
Click the upload button to upload the file to the server.
Is it possible to skip step 2 (the Upload button) and call the 'uploadFile_Click' function on the server through a jquery (postback) once a file is selected? I'm trying the following method but I need some help to finalize it.
ASPX PAGE
<asp:FileUpload runat="server" ID="UploadImages" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="UploadImages" ClientValidationFunction="ValidateUpload" />
CODE BEHIND
protected void uploadFile_Click(object sender, EventArgs e)
{
// Upload code
}
JQUERY
function ValidateUpload() {
var FileUpload_function = document.getElementById('UploadImages');
if (FileUpload_function.value == '') {
return false;
}
else {
** Here I want to trigger a postback to call the uploadFile_Click. Is that possible? **
}
return true;
}
Some (most?) browsers will not allow you to call the click event on an input HTML element if it is of type file (ie. an asp:fileupload control). It is considered a security risk.
I've created a method so that when a button (butCommit) is clicked, the value of a textbox is taken and stored into a string. That string is then written to a .txt file using the WriteAllText method. However, when this method is executed, the file is not modified at all.
The button method is working fine, as I have a response.redirect method in there which works every time.
The path for the .txt file is also correct as I have another method which will, on the page load, display the current contents of the .txt file (using the ReadAllText method) and the exact same path that I am using for the WriteAllText method.
Here is the code giving me problems:
void butCommit_Click(object sender, EventArgs e)
{
var path = Server.MapPath(#"~/content.txt");
string content = txtHomepageContent.Text;
System.IO.File.WriteAllText(path, content);
Response.Redirect("Default.aspx");
}
I'll repeat again: The above method is initialized and works fine with the Response.Redirect method, just not the WriteAllText method.
EDIT For more clarity:
The purpose of this is to display a message on the home page of a site. That message is required to be stored in a .txt file which is on the server (named content.txt). In the ControlPanel.aspx page, the user needs to be able to change the context of the text file using a text box. Preferably they are also able to view what is currently in content.txt using the same text box.
The code I have for the viewing bit is this:
protected void Page_Load(object sender, EventArgs e)
{
var path = Server.MapPath(#"~/content.txt");
string content = System.IO.File.ReadAllText(path);
txtHomepageContent.Text = content;
}
This specifies "path" as the path to content.txt, then specifies "content" as a string containing content.txt's data. After that it places "content" into the textbox txtHomepageContent. This is also the text box used to input the new data for content.txt.
It seems if I remove the above section of code, I am able to write to the text file now with no problems, however I of course am unable to view what is in there first. If I leave the above code chunk in, then whatever I write in the textbox is ignored, and the original text is submitted back into the content.txt, resulting in no changes.
I do understand there are easier ways of doing this such as storing in a database, but the requirements are for it to be in a .txt file.
You can either use the Page_Init event handler instead of Page_Load to initialize your text box (since submitted postback values will load between the Init and Load events, effectively overwriting the text box value with what was submitted), or if you want to stay with Page_Load, check whether there is a postback first:
protected void Page_Load(object sender, EventArgs e)
{
if (this.Page.IsPostBack)
return;
var path = Server.MapPath(#"~/content.txt");
string content = System.IO.File.ReadAllText(path);
txtHomepageContent.Text = content;
}
I just need the name of the file, but when I click the save button it kinda uploads the file (big files will produce an exception) and I don't want the page to "try" to upload the file.
Markup:
<asp:FileUpload runat="server" ID="txtPDFPath" />
Code Behind:
Reference r = new Reference() { R_CatalogNumber = txtCatalogNumber.Text.ToLower(), R_PDFPath = txtPDFPath.FileName.ToLower() };
So the workaround I figured out was to use a simple 'input type="file"' and a asp textbox with 'Style=""display:none"' and with some javascript set the text of the textbox to the file input value, note that you will get c:\fakepath\filename so you can use the Path class to get just the file name... it wokrs perfectly...
I'm currently trying to look at a directory, and then preview a .jpeg from a list box. I have the list box populating with the contents of the directory and only showing Jpegs, but I can't think of what to do to get the jpeg preview in a picture box. I'm using an asp .net application on Visual Studio 2010.
This is the code I have
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DirectoryInfo infoDir = new DirectoryInfo(#"G:\Test_Directory");
FileInfo[] infoFile = infoDir.GetFiles("*.jpeg");
foreach( FileInfo file in infoFile )
{
lstDirectory.Items.Add(file.Name);
}
}
protected void lstDirectory_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
I'm under the understanding Postback needs to be used. If anyone is able to help, that would be great.
The file which is in the G: Drive, is a jpeg, which can be seen in the list box is : jpegimage.jpeg
Thanks.
How about something like this?
I think you could do this mostly in Javascript, with two additional ASP.NET page.
First, create a new web page. We'll call this A.aspx. This page will be passed the image name in the query string. It will be very simple: it will just fetch the contents of the file from "G:\TestDirectory" and write it to the Response stream. There are quite a few questions and answers on Stack Overflow on how to do this, if you haven't done it before.
Then, create another web page. We'll call this B.aspx. This will have an image control with height and width set appropriately. It will also take the image name from its query string. The code-behind will build a URL to use as the ImageSource property on the image control. The URL will be that of A.aspx, with the (URL-encoded) image name appended as a parameter.
On your ASP.NET page, hook up an event handler to your listbox. When the selected index on the list box changes, on the client side, build a URL, based on the URL to B.aspx with the image name from the list box appended as a parameter. Then open a window, using the URL you just built, pointing to B and passing the desired file name.
So: when the list box selected index changes (or when you double click, or whatever event you pick), the javascript will open a window with page B.aspx. Page B will have an image control, set to the URL to A.aspx. A.aspx will stream the image contents to the image control, which will appear in your new window.
I have an asp:GridView control inside a .aspx page that the user can add several rows of data to. The user must also be able to attach a file to each row of data added.
For this I use the following inside the GridView:
<asp:TemplateField HeaderText="Upload" HeaderStyle-Width="120px">
<EditItemTemplate>
<asp:FileUpload ID="fuUploadLocation" runat="server" Width="98%" TabIndex="18" />
</EditItemTemplate>
</asp:TemplateField>
Then to save the location of the file upload I use the RowUpdating event in code-behind to set the value etc.
The problem is I can't register a PostBackTrigger for the control on the html as it doesn't pick it up as it's inside the GridView. I've tried setting it dynamically from other examples but can't seem to get this to work, the result being my FileUpload's FileName is always empty and the file then doesn't save correctly.
Any suggestions would be awesome.
Thanks
On Gridview row databound you have to find the file upload control and then add it to your UpdatePanel postback trigger.
I've not found a solution to my problem but I did work around it (sort of). I know just add a link to the grid, when user clicks it sends ID through via querystring, a new page opens that handles the entire upload process and returns the url of the saved document.
Not ideal but it works and saved me hours of frustration.
In the OnItemDataBound event handler of the grid need to call ScriptManager.GetCurrent(this).RegisterPostBackControl(ControlID);
This is an old post but for anyone that is stuck with it, you need to add the following to your control as stated in the other answers.
protected void ItemDataBound(object sender, EventArgs e)
{
Button myButton = (Button)e.Item.FindControl("myButton");
if (myButton != null)
ScriptManager.GetCurrent(Page).RegisterPostBackControl(myButton);
}
You also need to change the enctype in your form tag to the following e.g.:
protected void Page_PreRender(object sender, EventArgs e)
{
Page.Form.Attributes.Add("enctype", "multipart/form-data");
}
It should then work without an issue.