This question already has answers here:
Directory Chooser in HTML page
(8 answers)
Closed 7 years ago.
I want users to be able to save files to a certain location so I need to save the folder path they want. I want to do something like the way to change the download location in Google Chrome:
I know to save a file I could use <asp:FileUpload runat="server" ID="file" />
But I just need to capture the path of the folder, not save a file. How do I do this in asp.net? Then later when I do save the file, I will have the location the file needs to go.
i think this would help you some
first you need to import windows.forms to asp.net (aspx.cs) page.
using System.Windows.Forms;
then take a button and generate an event to it.
use the code below it will show save as dialog box.
SaveFileDialog my_Sfd1 = new SaveFileDialog();
my_Sfd1.Filter = "All files (*.*)|*.*"; //here you can specify the file format.(*.*)indicates all files.
my_Sfd1.Title = "Save file to"; //Title to display at top of the dialog box
my_Sfd1.FilterIndex = 2;
my_Sfd1.RestoreDirectory = true;
if (DialogResult.OK == (new Invoker(my_Sfd1).Invoke()))
{
//add file here which you want to save.
}
Related
If I want to create a button for template-editor I am using this code:
#Edit.Toolbar(actions: "template-develop")
But in my template code I also use code like this:
#RenderPage("_pager.cshtml", new { count = data.pCount, active = data.pActive})
And if I want to edit this file: _pager.cshtml I have to go to the server with FTP or RDP and change this file...
Can I and how create "template-editor" button for _pager.cshtml that I can edit it inside web browser?
Wonder why you need to open it through FTP or RDP as you can edit it directly from website by clicking the template name :
I have my views setup to pre-compile, and therefore, at runtime if I were to try and read the view file (e.g. "~\Views\User\Report.cshtml") I'd get the following dummy-text, as opposed to the contents of my view:
This is a marker file generated by the precompilation tool, and should not be deleted!
Problem is, I'd like to re-use the cshtml view, and rerender it another way at runtime, but I cannot due to the above restriction.
The scenario:
An admin can see a list of users in a /User/Report route. It outputs some HTML that has a list of all users, and their information in an HTML table. These admins frequently want to download this html file (styles and all) to email it as an attachment to someone else. They could, of course, go to File->Save in their browser, but I wanted to simplify that action by adding a link to the page "Download this report as HTML" that would simply return the same page's content, as a forced-downloaded HTML file (2012-07-11_UserReport.html).
So, what I tried to do was re-render the view by running the Report.cshtml file's contents through ASP.NET's File() method, like this:
var html = System.IO.File.ReadAllText(Server.MapPath(#"~\Views\User\Report.cshtml"));
var bytes = System.Text.Encoding.UTF8.GetBytes(html);
return File(bytes,"text/html",string.Format("{0}_UserReport.html",DateTime.Now.ToString("yyyy-MM-dd")));
But, like I mentioned earlier, the file comes back as the dummy-text, not the view, since I'm pre-compiling the views.
I understand that to get around the pre-compilition, I could simply copy the Report.cshtml file, and rename it to Report.uncompiled (adding it to the csproj as of course) and read the contents of it, that's an ok solution, but not good enough.
What I would really like to know is: Is there a way I can get at that pre-compiled content? I looked in the Assembly's embedded resources, and they are not there. Any ideas/suggestions?
Updated with current solution
So after searching around some more, and trying to use WebClient/WebRequest to just make a request to the route's URL and send the response back down to the user to download while at the same time trying to pass the user's .ASPXAUTH cookie (that made WebClient/WebRequest time out for some reason? I even tried to create a new ticket, same result) I ended up going with what I didn't want to do: duplicate the view file, and rename it so it's not precompiled.
The view file (Report.uncompiled) had to be modified a bit as it was, and then I ran it through RazorEngine's Razor.Parse method and got what I needed, but it just felt hackey. Would still like a way to access the view file (Report.cshtml) even after it's compiled.
var templateHtml = Razor.Parse(System.IO.File.ReadAllText(Server.MapPath(#"~\Views\User\Report.uncompiled")),model);
var bytes = System.Text.Encoding.UTF8.GetBytes(templateHtml);
return File(bytes, "text/html", string.Format("{0}_UserReport.html", DateTime.Now.ToString("yyyy-MM-dd")));
Would the WebClient class work?
using System.Net;
using (WebClient client = new WebClient ())
{
client.DownloadFile("http://yourwebsite.com/test.html", #"C:\directory.html");
// If you just want access to the html, see below
string html = client.DownloadString("http://yourwebsite.com/test.html");
}
Just have this fire whenever your user clicks a button and then it will save the current content of the page wherever? You could probably also have a directory selector and feed whatever they select into that second parameter.
It essentially does the same thing as the browser save as, if that's what you want.
Q.1. When a image file is uploaded via AjaxUplaoder of CuteWebUI.Uploader it saves file like this
persisted.057fe17e-9707-4f3a-91b7-250239b19c2f.10.JPG.resx
in which "10.jpg" is Image file name and Other "persisted.057fe17e-9707-4f3a-91b7-250239b19c2f.10.JPG.resx" I dont know what is this?
Kindly help me to extract file name from this given format "persisted.057fe17e-9707-4f3a-91b7-250239b19c2f.10.JPG.resx" so that I can show image on Image Control of ASP.Net that is uploaded image file via this Ajax Uploader. This ajax uploader's property 'args.filename' gives file name on 'FileUploaded' event.
look here for the solution of your problem: http://ajaxuploader.com/document/scr/html/How-to-save-uploaded-file.htm
I'm working with .net 4.0 in asp.net.
I have folder on web server having some pdf file, I display its (File Name) Name in gridview then i want to do "when i click on Item in grid view then it open that pdf file and generate url with file in browser."
I'm using Following Code
GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).Parent.Parent;
LinkButton hk = (LinkButton)gvFiles.Rows[row.RowIndex].FindControl("lnkbtnTitleView");
string s = Server.MapPath("~/AppName/App_" + dtFiles.Rows[0]["ENewsLetterID"].ToString() + "_1.PDF");
hk.Attributes.Add("onclick","window.open('"+s+"')");
Add ashx handler to your website, that will send pdf as content back to client. Add links to gridview, that will reference to this handler.
If the PDF file already exists simply put direct link:
string s = string.Format("/AppName/App_{0}_1.PDF", dtFiles.Rows[0]["ENewsLetterID"]);
And add window name _blank:
hk.Attributes.Add("onclick", string.Format("window.open('{0}', '_blank');", s));
Otherwise please give more details: how the PDF should be generated?
In my web application, I need to be able to allow users to upload and download their images. How can this be don in ASP.net?
I want to user to be able to sign in (I already have that done) and be able to upload images to their account. Later on, I want them to be able to download them.
Thanks
If the images are of a reasonable size, (e.g. less than a few MB), you can do this with a <input type=file> tag. Set the encoding of your form tag to multipart/form-data and implement some code-behind to get a hold of the file.
See the ASP.Net videos (Part 1) and (Part 2) for a detailed example.
Hope this helps!
You can also use
<asp:FileUpload runat="server" id="upImage" Width="575px" CssClass="txt-Normal"></asp:FileUpload>
Here is my method to save them to a location on the site.
string completeFilePath = this.Request.PhysicalApplicationPath + System.Configuration.ConfigurationManager.AppSettings["ImageUploadPath"];
if (System.IO.Directory.Exists(completeFilePath))
{
System.IO.Directory.CreateDirectory(completeFilePath);
}
if (!System.IO.File.Exists(completeFilePath + this.upImage.FileName))
{
this.upImage.SaveAs(completeFilePath + this.upImage.FileName);
}
imageUrl = string.Format("~{0}{1}", System.Configuration.ConfigurationManager.AppSettings["ImageUploadPath"], this.upImage.FileName);