Showing posts with label upload. Show all posts
Showing posts with label upload. Show all posts

Thursday, March 29, 2012

Newbie: How to upload and save images using ASP.NET web forms (C#)

Hello -

I need to upload images in my web application using ASP.NET web forms
(written in C#). Since i have never done this before, i am unsure what is the
best method.

I assume there are two options:

1). Upload the images and save the files directory to the windows file
system. However, i assume this poses a security risk, as the file directory
will require write permissions.

2). Save the images (as objects?) in the SQL SERVER database.

Can anyone suggest the best way to do this? And, if possible, point me to
some sample code or artciles which explain how to do this.

Thanks!Charlie,

check out the following article:

http://www.codeproject.com/aspnet/fileupload.asp

I prefer to save files in the database but it depends on your needs.

If you need to save hundred thousands of files and decide that you want to
save them on your hard disk make sure to distribute them on different
directories. What you could do is that you assign a guid to each file and
create your directory structure accordingly.

Example:

You have five files with the following guids

0A59E8D6-0AE5-475B-8698-814C2C712029

F16CE1B1-56D0-42E6-9463-A58D553CC289

D90C27C7 -8BD9-4F47-8CFE-DE9D240AD136

370B6080-DF06-4DDE-BB5E-85264B8AE48B

861DC387-FDC7-4A4F-B0C3-9EA6734A562A

You could now create a directory like the following:

0A59E8D6\0AE5\475B\8698\814C2C712029

F16CE1B1\56D0\42E6\9463\A58D553CC289

D90C27C7 \8BD9\4F47\8CFE\DE9D240AD136

370B6080\DF06\4DDE\BB5E\85264B8AE48B

861DC387\FDC7\4A4F\B0C3\9EA6734A562A

HTHs

Daniel

"charliewest" <charliewest@.discussions.microsoft.com> schrieb im Newsbeitrag
news:E6DD7087-DC4F-4E78-9741-0EF7F76883B6@.microsoft.com...
> Hello -
> I need to upload images in my web application using ASP.NET web forms
> (written in C#). Since i have never done this before, i am unsure what is
> the
> best method.
> I assume there are two options:
> 1). Upload the images and save the files directory to the windows file
> system. However, i assume this poses a security risk, as the file
> directory
> will require write permissions.
> 2). Save the images (as objects?) in the SQL SERVER database.
> Can anyone suggest the best way to do this? And, if possible, point me to
> some sample code or artciles which explain how to do this.
> Thanks!

Monday, March 26, 2012

newbie: preparing ASP.NET application for release

I have an ASP.NET application that I'm ready to upload onto a webserver
in a release format.

I just want to be sure of a few things first:

- I take it that I only need to upload the aspx files, the bin directory
files, stylesheets, global.asax and web.config onto the server?

(i.e. I don't need to upload the .vb source files and assemblynfo.vb,
etc?)

- Also, to prepare the application for release mode, do I just need to
set the <compilation> tag to debug="false" ?

Anything else I should know about?

Thanks,
Peter
--

"I hear ma train a comin'
... hear freedom comin"debug="false" compile the code i release mode (assuming you are using
VS.Net, that's just a dropdown that says DEBUG and RELEASE). you also need
ascx (user controls), asmx (web services), asax (global)

you can remove the vbproj and sln/suo files.

karl
--
MY ASP.Net tutorials
http://www.openmymind.net/

"Stimp" <ren@.spumco.com> wrote in message
news:slrncphetf.sp2.ren@.carbon.redbrick.dcu.ie...
> I have an ASP.NET application that I'm ready to upload onto a webserver
> in a release format.
> I just want to be sure of a few things first:
> - I take it that I only need to upload the aspx files, the bin directory
> files, stylesheets, global.asax and web.config onto the server?
> (i.e. I don't need to upload the .vb source files and assemblynfo.vb,
> etc?)
> - Also, to prepare the application for release mode, do I just need to
> set the <compilation> tag to debug="false" ?
> Anything else I should know about?
> Thanks,
> Peter
> --
> "I hear ma train a comin'
> ... hear freedom comin"
To depoly the project to the server you can use the copy project function in
the project menu (if you are using Visual Studio). Just check, copy "Only
files needed to run this application". You must have your ASP.NET project
selected in the solution explorer.

/Henke

"Stimp" <ren@.spumco.com> skrev i meddelandet
news:slrncphetf.sp2.ren@.carbon.redbrick.dcu.ie...
>I have an ASP.NET application that I'm ready to upload onto a webserver
> in a release format.
> I just want to be sure of a few things first:
> - I take it that I only need to upload the aspx files, the bin directory
> files, stylesheets, global.asax and web.config onto the server?
> (i.e. I don't need to upload the .vb source files and assemblynfo.vb,
> etc?)
> - Also, to prepare the application for release mode, do I just need to
> set the <compilation> tag to debug="false" ?
> Anything else I should know about?
> Thanks,
> Peter
> --
> "I hear ma train a comin'
> ... hear freedom comin"
... in addition to what guys suggested check out this:
http://msdn.microsoft.com/library/d.../CL_SecuAsp.asp
Pay attention to web.config settings, especially if your app is hosted on
shared server (hosting company or several apps on one server)

"Stimp" <ren@.spumco.com> wrote in message
news:slrncphetf.sp2.ren@.carbon.redbrick.dcu.ie...
>I have an ASP.NET application that I'm ready to upload onto a webserver
> in a release format.
> I just want to be sure of a few things first:
> - I take it that I only need to upload the aspx files, the bin directory
> files, stylesheets, global.asax and web.config onto the server?
> (i.e. I don't need to upload the .vb source files and assemblynfo.vb,
> etc?)
> - Also, to prepare the application for release mode, do I just need to
> set the <compilation> tag to debug="false" ?
> Anything else I should know about?
> Thanks,
> Peter
> --
> "I hear ma train a comin'
> ... hear freedom comin"

newbie: preparing ASP.NET application for release

I have an ASP.NET application that I'm ready to upload onto a webserver
in a release format.
I just want to be sure of a few things first:
- I take it that I only need to upload the aspx files, the bin directory
files, stylesheets, global.asax and web.config onto the server?
(i.e. I don't need to upload the .vb source files and assemblynfo.vb,
etc?)
- Also, to prepare the application for release mode, do I just need to
set the <compilation> tag to debug="false" ?
Anything else I should know about?
Thanks,
Peter
--
"I hear ma train a comin'
... hear freedom comin"debug="false" compile the code i release mode (assuming you are using
VS.Net, that's just a dropdown that says DEBUG and RELEASE). you also need
ascx (user controls), asmx (web services), asax (global)
you can remove the vbproj and sln/suo files.
karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
"Stimp" <ren@.spumco.com> wrote in message
news:slrncphetf.sp2.ren@.carbon.redbrick.dcu.ie...
> I have an ASP.NET application that I'm ready to upload onto a webserver
> in a release format.
> I just want to be sure of a few things first:
> - I take it that I only need to upload the aspx files, the bin directory
> files, stylesheets, global.asax and web.config onto the server?
> (i.e. I don't need to upload the .vb source files and assemblynfo.vb,
> etc?)
> - Also, to prepare the application for release mode, do I just need to
> set the <compilation> tag to debug="false" ?
> Anything else I should know about?
> Thanks,
> Peter
> --
> "I hear ma train a comin'
> ... hear freedom comin"
To depoly the project to the server you can use the copy project function in
the project menu (if you are using Visual Studio). Just check, copy "Only
files needed to run this application". You must have your ASP.NET project
selected in the solution explorer.
/Henke
"Stimp" <ren@.spumco.com> skrev i meddelandet
news:slrncphetf.sp2.ren@.carbon.redbrick.dcu.ie...
>I have an ASP.NET application that I'm ready to upload onto a webserver
> in a release format.
> I just want to be sure of a few things first:
> - I take it that I only need to upload the aspx files, the bin directory
> files, stylesheets, global.asax and web.config onto the server?
> (i.e. I don't need to upload the .vb source files and assemblynfo.vb,
> etc?)
> - Also, to prepare the application for release mode, do I just need to
> set the <compilation> tag to debug="false" ?
> Anything else I should know about?
> Thanks,
> Peter
> --
> "I hear ma train a comin'
> ... hear freedom comin"
... in addition to what guys suggested check out this:
http://msdn.microsoft.com/library/d.../>
SecuAsp.asp
Pay attention to web.config settings, especially if your app is hosted on
shared server (hosting company or several apps on one server)
"Stimp" <ren@.spumco.com> wrote in message
news:slrncphetf.sp2.ren@.carbon.redbrick.dcu.ie...
>I have an ASP.NET application that I'm ready to upload onto a webserver
> in a release format.
> I just want to be sure of a few things first:
> - I take it that I only need to upload the aspx files, the bin directory
> files, stylesheets, global.asax and web.config onto the server?
> (i.e. I don't need to upload the .vb source files and assemblynfo.vb,
> etc?)
> - Also, to prepare the application for release mode, do I just need to
> set the <compilation> tag to debug="false" ?
> Anything else I should know about?
> Thanks,
> Peter
> --
> "I hear ma train a comin'
> ... hear freedom comin"

Saturday, March 24, 2012

Newbie: Upload large file from client to server in ASP.NET

I have seen many posts for this same subject, but my project has some
odd requirements. I need to develop an ASP app that will upload a
large file (200 MB+) to a server without any user interaction, meaning
I can's simply put a file input box on the page and let the user
choose the file. I basically need them to click a button and have the
file transferred. I know that this would be a dangerous practice if
anyone could do it, but is it possible in a controlled environment?
Because of the size of the file I have been looking at FTPing it, but
can't figure out how to get client script to start the FTP. I have
also found this code (below) that utilizes a web service, but I don't
see how the client-script would get the file from the client since it
appears to be ASP. Any ideas for this situation would be MUCH
appreciated.

<WebMethod()> PublicFunction UploadFile(ByVal fs()AsByte,ByVal
FlNameAsString)AsString
Try
Dim m As New MemoryStream(fs)
Dim f As New FileStream("c:\tempUploaded\" & FlName,
FileMode.Create)
m.WriteTo(f)
m.Close()
f.Close()
f = Nothing
m = Nothing
Return "File Uploaded"

Catch ex As Exception
Return ex.Message
End Try
End Function

The UploadFile Webmethod accepts a byte array and the name of the
file, the byte array is copied to a memory stream which is written to
a FileStream.

The client code to upload a file,

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Dim f As System.IO.File
Dim fs As System.IO.FileStream
Dim o As New localhost.Service1()
'file to upload
fs = f.Open("c:\upload.pdf", IO.FileMode.Open, IO.FileAccess.Read)
Dim b(fs.Length - 1) As Byte
fs.Read(b, 0, fs.Length)
MsgBox(o.UploadFile(b, "upload.pdf"))
f = Nothing
fs.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End SubYou can use the HTTP classes to write an upload client as a Winexe, there
are examples in the SDK of how to do this using the webclient uploadfile
method, as shown here.

Console.Write("\nPlease enter the URL to post data to : ");
String uriString = Console.ReadLine();

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();

Console.WriteLine("\nPlease enter the fully qualified path of the
file to be uploaded to the URL");
string fileName = Console.ReadLine();

Console.WriteLine("Uploading {0} to {1} ...",fileName,uriString);
// Upload the file to the URL using the HTTP 1.0 POST.
byte[] responseArray =
myWebClient.UploadFile(uriString,"POST",fileName);

// Decode and display the response.
Console.WriteLine("\nResponse Received.The contents of the file
uploaded are: \n{0}",Encoding.ASCII.GetString(responseArray));

You then need to control your server side timouts, and set your
machine.config settings to control the ma file size permitted for uplaods
(look it up - cant recall all the settings)

If you want to resort to FTP, then you can easily script the FTP.EXE that
comes with windows with the /s flag.

--
Regards

John Timney (Microsoft ASP.NET MVP)
--------------
<shameless_author_plug>
Professional .NET for Java Developers with C#
ISBN:1-861007-91-4
Professional Windows Forms
ISBN: 1861005547
Professional JSP 2nd Edition
ISBN: 1861004958
Professional JSP
ISBN: 1861003625
Beginning JSP Web Development
ISBN: 1861002092
</shameless_author_plug>
--------------

"Greg" <greg_leflar@.hotmail.com> wrote in message
news:1651b2f9.0308171354.6c1ee8ae@.posting.google.c om...
> I have seen many posts for this same subject, but my project has some
> odd requirements. I need to develop an ASP app that will upload a
> large file (200 MB+) to a server without any user interaction, meaning
> I can's simply put a file input box on the page and let the user
> choose the file. I basically need them to click a button and have the
> file transferred. I know that this would be a dangerous practice if
> anyone could do it, but is it possible in a controlled environment?
> Because of the size of the file I have been looking at FTPing it, but
> can't figure out how to get client script to start the FTP. I have
> also found this code (below) that utilizes a web service, but I don't
> see how the client-script would get the file from the client since it
> appears to be ASP. Any ideas for this situation would be MUCH
> appreciated.
> <WebMethod()> PublicFunction UploadFile(ByVal fs()AsByte,ByVal
> FlNameAsString)AsString
> Try
> Dim m As New MemoryStream(fs)
> Dim f As New FileStream("c:\tempUploaded\" & FlName,
> FileMode.Create)
> m.WriteTo(f)
> m.Close()
> f.Close()
> f = Nothing
> m = Nothing
> Return "File Uploaded"
> Catch ex As Exception
> Return ex.Message
> End Try
> End Function
> The UploadFile Webmethod accepts a byte array and the name of the
> file, the byte array is copied to a memory stream which is written to
> a FileStream.
> The client code to upload a file,
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
> Try
> Dim f As System.IO.File
> Dim fs As System.IO.FileStream
> Dim o As New localhost.Service1()
> 'file to upload
> fs = f.Open("c:\upload.pdf", IO.FileMode.Open, IO.FileAccess.Read)
> Dim b(fs.Length - 1) As Byte
> fs.Read(b, 0, fs.Length)
> MsgBox(o.UploadFile(b, "upload.pdf"))
> f = Nothing
> fs.Close()
> Catch ex As Exception
> MsgBox(ex.Message)
> End Try
> End Sub
Thank you for your reply. Your code sample makes sense. How would I
then execute that script on the client from an aspx? Again, I don't
know if this is even possible because of the devious implications, but
the client is on our LAN so I have complete control.

"John Timney \(Microsoft MVP\)" <timneyj@.despammed.com> wrote in message news:<OT1XywQZDHA.2668@.TK2MSFTNGP09.phx.gbl>...
> You can use the HTTP classes to write an upload client as a Winexe, there
> are examples in the SDK of how to do this using the webclient uploadfile
> method, as shown here.
> Console.Write("\nPlease enter the URL to post data to : ");
> String uriString = Console.ReadLine();
> // Create a new WebClient instance.
> WebClient myWebClient = new WebClient();
> Console.WriteLine("\nPlease enter the fully qualified path of the
> file to be uploaded to the URL");
> string fileName = Console.ReadLine();
> Console.WriteLine("Uploading {0} to {1} ...",fileName,uriString);
> // Upload the file to the URL using the HTTP 1.0 POST.
> byte[] responseArray =
> myWebClient.UploadFile(uriString,"POST",fileName);
> // Decode and display the response.
> Console.WriteLine("\nResponse Received.The contents of the file
> uploaded are: \n{0}",Encoding.ASCII.GetString(responseArray));
> You then need to control your server side timouts, and set your
> machine.config settings to control the ma file size permitted for uplaods
> (look it up - cant recall all the settings)
> If you want to resort to FTP, then you can easily script the FTP.EXE that
> comes with windows with the /s flag.
> --
> Regards
> John Timney (Microsoft ASP.NET MVP)
> --------------
> <shameless_author_plug>
> Professional .NET for Java Developers with C#
> ISBN:1-861007-91-4
> Professional Windows Forms
> ISBN: 1861005547
> Professional JSP 2nd Edition
> ISBN: 1861004958
> Professional JSP
> ISBN: 1861003625
> Beginning JSP Web Development
> ISBN: 1861002092
> </shameless_author_plug>
> --------------
> "Greg" <greg_leflar@.hotmail.com> wrote in message
> news:1651b2f9.0308171354.6c1ee8ae@.posting.google.c om...
> > I have seen many posts for this same subject, but my project has some
> > odd requirements. I need to develop an ASP app that will upload a
> > large file (200 MB+) to a server without any user interaction, meaning
> > I can's simply put a file input box on the page and let the user
> > choose the file. I basically need them to click a button and have the
> > file transferred. I know that this would be a dangerous practice if
> > anyone could do it, but is it possible in a controlled environment?
> > Because of the size of the file I have been looking at FTPing it, but
> > can't figure out how to get client script to start the FTP. I have
> > also found this code (below) that utilizes a web service, but I don't
> > see how the client-script would get the file from the client since it
> > appears to be ASP. Any ideas for this situation would be MUCH
> > appreciated.
> > <WebMethod()> PublicFunction UploadFile(ByVal fs()AsByte,ByVal
> > FlNameAsString)AsString
> > Try
> > Dim m As New MemoryStream(fs)
> > Dim f As New FileStream("c:\tempUploaded\" & FlName,
> > FileMode.Create)
> > m.WriteTo(f)
> > m.Close()
> > f.Close()
> > f = Nothing
> > m = Nothing
> > Return "File Uploaded"
> > Catch ex As Exception
> > Return ex.Message
> > End Try
> > End Function
> > The UploadFile Webmethod accepts a byte array and the name of the
> > file, the byte array is copied to a memory stream which is written to
> > a FileStream.
> > The client code to upload a file,
> > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> > System.EventArgs) Handles Button1.Click
> > Try
> > Dim f As System.IO.File
> > Dim fs As System.IO.FileStream
> > Dim o As New localhost.Service1()
> > 'file to upload
> > fs = f.Open("c:\upload.pdf", IO.FileMode.Open, IO.FileAccess.Read)
> > Dim b(fs.Length - 1) As Byte
> > fs.Read(b, 0, fs.Length)
> > MsgBox(o.UploadFile(b, "upload.pdf"))
> > f = Nothing
> > fs.Close()
> > Catch ex As Exception
> > MsgBox(ex.Message)
> > End Try
> > End Sub