i want to upload files to the server thru asp.net. For this iam having some
values in db. say for example iam getting 4 values from db then 4 fileupload
controls should be there in the page (<input type="file" id="dynamicall
created" runat="server">). now i should be able to get all the four values
from this fileupload control (image path) and i should be able to save this
4 files to the server. is streaming is possible in this?
how can i do this. please advice.
regards
Venkat.G
From:
http://www.dotnet101.com/articles/art030_UploadFile.asp
There are two objects that you will use to upload files. One is the
INPUT control in the Web Browser and the other is the HtmlInputFile
object in the server. The INPUT control will be embedded inside a form
that will post the information encoded in a format suitable for a file
transfer over the Internet.
The INPUT control will also load the selected file into the Web
Browser's memory, and will allow access to the HtmlInputFile object in
the code that runs in the server through the INPUT control's ID. I.e.,
you use the ID of the INPUT control as the reference to the
HtmlInputFile object in the code that runs at the server.
To upload a file, the form must post the data with the code format
multiform/form-data instead of the default
application/x-www-form-urlencoded that sends the form elements in a
name/value pair base. This restriction arises from the types of
formats that an object of the class HtmlInputFile is
capable of processing and the characters that can traverse the
Internet without confusing the network devices and software. The
following example shows how to set the encoding format of a form:
<input type=file id=loFile enctype="multipart/form-data"
Once the data is submitted to the server, the object of the class
HtmlInputFile exposes the property PostedFile, with an object of the
type HttpPostedFile, that contains the raw file stream and the
attributes of the file, such as, file length, file type, file name.
But most important, the PostedFile object has the SaveAs method that
allows to save the uploaded file to a local directory in the web
server. You can also limit the type of the files you will allow users
to upload by setting the Access property of the HtmlInputFile object,
and you can also discard files base on the length by using the
PostedFile.ContentLength property. The following example shows how to
restrict the upload of video files. The ID loFile is the ID of the
INPUT control.
loFile.Acess = "video/*"
The example that follows shows how to enable a web form for file
uploads and how to answer back to the user with a message and
information about the file that was just uploaded. The web form is
contained in a panel (FileUploadForm) that is initially visible and
the answer is presented in a panel (AnswerMsg) that is initially
disable. Once the file is uploaded, the web form panel is
set to invisible and the message panel is set to visible.
The event that handles the submitted form runs at the server as
indicated by the Submit INPUT tag:
<input type="submit" value="Upload" OnServerClick="UploadFile_Clicked"
runat="server" ID=Submit1>
The Directory and File classes allow to create a folder to contain the
uploaded files and to get the name of the uploaded file filtering the
path. In order to use this classes we need to include the System.IO
name space, we can do so by including the import directive. This entry
allows the upload the name space into the web form application, for
more information on this topic read the article How to Manipulate
Files in ASP.NET:
<%@ Import Namespace="System.IO" %>
For those of you that were familiar with uploading files in ASP, the
HtmlInputFile class free us from the need of third party components to
process the multipart data stream.
<%@ Import Namespace="System.IO" %>
<html>
<SCRIPT LANGUAGE="VB" RUNAT="SERVER" ID=SCRIPT1>
Sub UploadFile_Clicked(Sender as Object, e as EventArgs)
Dim lstrFileName as string
Dim lstrFileNamePath as string
Dim lstrFileFolder as string
lstrFileFolder = "C:\TempUploadedFiles\"
'Gets the file name
lstrFileName = loFile.PostedFile.FileName
lstrFileName = File.GetFileNameFromPath(lstrFileName)
'Creates the folder if it does not exists
If (not Directory.DirectoryExists(lstrFileFolder)) Then
Directory.CreateDirectories(lstrFileFolder)
End If
'Save Uploaded file to server
lstrFileNamePath = lstrFileFolder & lstrFileName
loFile.PostedFile.SaveAs(lstrFileNamePath)
'Sets the answer
FileName.Text = lstrFileName
FileType.Text = loFile.PostedFile.ContentType
FileLength.Text = cStr(loFile.PostedFile.ContentLength)
FileUploadForm.visible = false
AnswerMsg.visible = true
End sub
</SCRIPT>
<BODY>
<ASP:panel id="FileUploadForm" visible="true" runat="server">
<form action="UploadFile.aspx" method="post"
enctype="multipart/form-data"
runat="server" ID=Form1>
<h1>ASP.NET File Upload Example</h1>
Select the File to Upload to the Server:<br>
<input id="loFile" type="file" runat="server"><br>
<input type="submit" value="Upload"
OnServerClick="UploadFile_Clicked"
runat="server"
ID=Submit1>
</form>
</ASP:panel>
<ASP:panel id="AnswerMsg" visible="false" runat="server">
Thanks your file <ASP:label id="FileName" runat="server" />
has been uploaded<BR>
We received <ASP:label id="FileLength" runat="server" />
bytes of the type <ASP:label id="FileType" runat="server" />
</ASP:panel>
</BODY>
</HTML>
"GVenkat" <gven...@msn.com> wrote:
>i want to upload files to the server thru asp.net. For this iam having some
>values in db. say for example iam getting 4 values from db then 4 fileupload
>controls should be there in the page (<input type="file" id="dynamicall
>created" runat="server">). now i should be able to get all the four values
>from this fileupload control (image path) and i should be able to save this
>4 files to the server. is streaming is possible in this?
Jim Ross
MS MVP [ASP.NET][VC/MFC emeritus]
To send email, change 'lotsofspamthroughhere' to 'msn' but please ask all questions in the newsgroups, not via private mail
http://www.wimdows.net/articles/article.aspx?aid=4
How you build your form from the DB though is down to you, but it should
certainly help you achive most of what you are seeking.
--
Regards
John Timney (Microsoft ASP.NET MVP)
----------------------------------------------
<shameless_author_plug>
Professional Windows Forms
ISBN: 1861005547
Professional JSP
ISBN: 1861003625
Beginning JSP Web Development
ISBN: 1861002092
</shameless_author_plug>
----------------------------------------------
"GVenkat" <gven...@msn.com> wrote in message
news:uT4m7zRGCHA.1904@tkmsftngp08...
thanx for you info. its useful for me.
regards
Venkat.G
"John Timney (Microsoft MVP)" <xyzti...@btinternet.com> wrote in message
news:uq93CvUGCHA.2680@tkmsftngp12...
thanx and it really helps me. one thing. is it possible to show the image as
preview to the user and if the user select upload then only the image should
save in the server or it won't, then the user can select another image. is
it possible. can we do this using stream objects. pl advice.
regards,
Venkat.G
"John Timney (Microsoft MVP)" <xyzti...@btinternet.com> wrote in message
news:uq93CvUGCHA.2680@tkmsftngp12...