I have a website - on quite a few of the pages there is a file upload box which saves the file and emails a confirmation.
Code is something like
if fileupload1.hasfile then
fileupload1.saveas(varfilename)
etc
end if
I'd love to put the code in the app_code folder so I dont have to repeat it. However, is it possible to access the file and the control from the class? How do I do that..
Hello, bro
Sometimes we need to make a class that usually contains our global resources to avoid the duplication in our implementation as you mentioned above. Let us now to show you by simple steps how make that, First we need to add App_Code folder
to Solution Explorer, then we should add also a class to App_Code folder to add our code inside it.
1- Here we will add this code inside the class that called UploadUtilities.
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public class UploadUtilities
{
public static bool UploadFile(FileUpload filectrl, string path)
{
if (File.Exists(path) == false && filectrl.HasFile)
{
filectrl.SaveAs(path);
return true;
}
else
{ return false; }
}
}
2- Second step we will add this tags in source page
I wouldnt access a fileupload control in a class file, rather I would create a method in class file which will accept a FileStrean, and then call this method using FileUpload.PostedFile.InputStream, and then use perform action on that stream...
pcrymble
Member
84 Points
44 Posts
calling an app_code class
Nov 17, 2012 12:10 PM|LINK
Hi there
I have a website - on quite a few of the pages there is a file upload box which saves the file and emails a confirmation.
Code is something like
if fileupload1.hasfile then
fileupload1.saveas(varfilename)
etc
end if
I'd love to put the code in the app_code folder so I dont have to repeat it. However, is it possible to access the file and the control from the class? How do I do that..
Thanks
Peter
grwlgrv
Member
51 Points
74 Posts
Re: calling an app_code class
Nov 17, 2012 12:18 PM|LINK
write a functin on tha same page as
public void savefile(FileUpload fld)
{
if fld.hasfile then
fld.saveas(varfilename)
etc
}
you can use this function in any other page by creating an object of that page or by inheriting that page.
Mohamed Elza...
Member
104 Points
17 Posts
Re: calling an app_code class
Nov 17, 2012 01:14 PM|LINK
Hello, bro
Sometimes we need to make a class that usually contains our global resources to avoid the duplication in our implementation as you mentioned above. Let us now to show you by simple steps how make that, First we need to add App_Code folder
to Solution Explorer, then we should add also a class to App_Code folder to add our code inside it.
1- Here we will add this code inside the class that called UploadUtilities.
using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public class UploadUtilities { public static bool UploadFile(FileUpload filectrl, string path) { if (File.Exists(path) == false && filectrl.HasFile) { filectrl.SaveAs(path); return true; } else { return false; } } }2- Second step we will add this tags in source page
<form id="form1" runat="server"> <div> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Upload" /> </div> </form>3- Third step we will add this in code-behind
protected void Button1_Click(object sender, EventArgs e) { bool status = UploadUtilities.UploadFile(FileUpload1, Server.MapPath("~/Images/" + FileUpload1.FileName)); if (status == true) { Response.Write("File Has Been Uploaded"); } else { Response.Write("File Exist"); } }Finally, if you face any problems feel free to ask me again.
Mohamed Elzayat
Please remember to Mark the replies as Answers if they help.
Facebook
Ruchira
All-Star
43056 Points
7040 Posts
MVP
Re: calling an app_code class
Nov 17, 2012 02:13 PM|LINK
Hello,
Not really.
Instead, put it in a user control. Check on the below links for that
http://msdn.microsoft.com/en-us/library/wt3k2fyw(v=vs.100).aspx
http://www.c-sharpcorner.com/uploadfile/jayendra/how-to-create-user-control-in-Asp-Net/
My Tech blog | My YouTube ChannelPlease 'Mark as Answer' if this post helps you.ramiramilu
All-Star
95503 Points
14106 Posts
Re: calling an app_code class
Nov 17, 2012 02:36 PM|LINK
I wouldnt access a fileupload control in a class file, rather I would create a method in class file which will accept a FileStrean, and then call this method using FileUpload.PostedFile.InputStream, and then use perform action on that stream...
thanks,
JumpStart
pcrymble
Member
84 Points
44 Posts
Re: calling an app_code class
Nov 21, 2012 07:49 AM|LINK
Thanks - really helpful
Peter