Monday, February 2, 2009

Code for Downloading and Uploading Files in

Code for downloading files ::
Sub Page_Load(Sender As Object, E As EventArgs)
Dim strRequest As String = Request.QueryString("file") '-- if something was passed to the file querystring If strRequest <> "" Then 'get absolute path of the file Dim path As String = Server.MapPath(strRequest) 'get file object as FileInfo Dim file As System.IO.FileInfo = New System.IO.FileInfo(path) '-- if the file exists on the server If file.Exists Then 'set appropriate headers Response.Clear() Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name) Response.AddHeader("Content-Length", file.Length.ToString()) Response.ContentType = "application/octet-stream" Response.WriteFile(file.FullName) Response.End 'if file does not exist Else Response.Write("This file does not exist.") End If 'nothing in the URL as HTTP GET Else Response.Write("Please provide a file to download.")
End If
End Sub
Code for uploading the Files::
in markup side::-
asp:FileUpLoad id="FileUpLoad1" AlternateText="You cannot upload files" runat="server"
asp:Button id="Button1" Text="Upload" OnClick="Button1_Click" runat="server"
asp:Label id="Label1" runat="server"
---------------------in CodeBehind::
Sub Button1_Click(sender As Object, e As EventArgs) if FileUpLoad1.HasFile 'Uncomment this line to Save the uploaded file 'FileUpLoad1.SaveAs("C:\SomePhysicalPath" & FileUpLoad1.Filename) Label1.Text = "Received " & FileUpLoad1.FileName & " Content Type " & FileUpLoad1.PostedFile.ContentType & " Length " & FileUpLoad1.PostedFile.ContentLength else Label1.Text = "No uploaded file" end if end sub

No comments:

Post a Comment