473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Gaining access to local files from an ASP.Net page

Hi all

I am writing a webpage which asks the user for the name of a picture
file, then reads the file, resizes if necessary and saves it to the
server. The code to read the file is...

' Set permissions for loaded file.
Dim FileIOPerm1 As
System.Security.Permissions.FileIOPermission
FileIOPerm1 = New
System.Security.Permissions.FileIOPermission(Permi ssionState.Unrestricted,
Me.File1.PostedFile.FileName())
FileIOPerm1.AllLocalFiles =
FileIOPermissionAccess.AllAccess
FileIOPerm1.Assert()

' Load file into memory.
Dim FullSizeImage As System.Drawing.Image
FullSizeImage =
System.Drawing.Image.FromFile(Me.File1.PostedFile. FileName())

Me.File1.PostedFile.FileName() holds the file name, which has the full
path, is valid and can be anywhere locally. This all works fine on my
development machine (without the need for the FileIOPermission object)
running direct from VS2003, but when run from the web I get the
following error...

Request for the permission of type
System.Security.Permissions.FileIOPermission, mscorlib,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
failed.

Now I can see the value of not making it easy to see local files from
any old webpage, so my question is how can this be achieved? As a
model I am thinking of Hotmail, where you can specfiy files as
attachments without going through any security hoops.

Thanks in advance.

Dave

Jul 19 '06 #1
6 1796
Dave,

Your code is good on server side. It works on your development machine
because it's the server. You should use a file upload element <input
type=file ...>. It provides a file selection dialog with no need for extra
security setting.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]

"Dave Keen" <da********@hotmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Hi all

I am writing a webpage which asks the user for the name of a picture
file, then reads the file, resizes if necessary and saves it to the
server. The code to read the file is...

' Set permissions for loaded file.
Dim FileIOPerm1 As
System.Security.Permissions.FileIOPermission
FileIOPerm1 = New
System.Security.Permissions.FileIOPermission(Permi ssionState.Unrestricted,
Me.File1.PostedFile.FileName())
FileIOPerm1.AllLocalFiles =
FileIOPermissionAccess.AllAccess
FileIOPerm1.Assert()

' Load file into memory.
Dim FullSizeImage As System.Drawing.Image
FullSizeImage =
System.Drawing.Image.FromFile(Me.File1.PostedFile. FileName())

Me.File1.PostedFile.FileName() holds the file name, which has the full
path, is valid and can be anywhere locally. This all works fine on my
development machine (without the need for the FileIOPermission object)
running direct from VS2003, but when run from the web I get the
following error...

Request for the permission of type
System.Security.Permissions.FileIOPermission, mscorlib,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
failed.

Now I can see the value of not making it easy to see local files from
any old webpage, so my question is how can this be achieved? As a
model I am thinking of Hotmail, where you can specfiy files as
attachments without going through any security hoops.

Thanks in advance.

Dave

Jul 19 '06 #2
Hi Eliyahu

Thanks for your input. I already am using a file upload element on my
page...

<TABLE id="tblMain" cellSpacing="0" cellPadding="5" width="280"
border="0">
<TR>
<TD><asp:label id="lblSelectAFile" runat="server">Select a file to
upload</asp:label></TD>
</TR>
<TR>
<TD><input id="File1" style="WIDTH: 261px; HEIGHT: 22px" type="file"
size="24" name="File1" runat="server"></TD>
</TR>
<TR>
<TD align="center"><asp:button id="cmdUpload" runat="server"
Width="90px" Text="Upload" BackColor="#DFD799"></asp:button></TD>
</TR>
</TABLE>

This gives me the Browse button and works a treat, so I can effectively
see the directory structure etc. However it is once I have captured
the name of the file that I have problems in loading it up. Is there
some feature that I am missing?

Thanks

Dave

Jul 19 '06 #3
Once you selected the name, all you need is just to submit the form. The
file upload element will take care of uploading the file.
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]

"Dave Keen" <da********@hotmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
Hi Eliyahu

Thanks for your input. I already am using a file upload element on my
page...

<TABLE id="tblMain" cellSpacing="0" cellPadding="5" width="280"
border="0">
<TR>
<TD><asp:label id="lblSelectAFile" runat="server">Select a file to
upload</asp:label></TD>
</TR>
<TR>
<TD><input id="File1" style="WIDTH: 261px; HEIGHT: 22px" type="file"
size="24" name="File1" runat="server"></TD>
</TR>
<TR>
<TD align="center"><asp:button id="cmdUpload" runat="server"
Width="90px" Text="Upload" BackColor="#DFD799"></asp:button></TD>
</TR>
</TABLE>

This gives me the Browse button and works a treat, so I can effectively
see the directory structure etc. However it is once I have captured
the name of the file that I have problems in loading it up. Is there
some feature that I am missing?

Thanks

Dave

Jul 19 '06 #4
Hi

OK, it seems I have tried to be too clever, and thought that simply
creating an image object using the filename of the upload file would be
OK, but it seems to invoke all those permission problems. Looks like I
will have to look into how to use the httppostedfile object instead.

Many thanks for your help.

Dave

Jul 19 '06 #5
Dave,
You got it. HttpPostedFile as I recall has a Stream property which you can
convert to either a byte array or use the Image.FromStream method at the
server.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Dave Keen" wrote:
Hi

OK, it seems I have tried to be too clever, and thought that simply
creating an image object using the filename of the upload file would be
OK, but it seems to invoke all those permission problems. Looks like I
will have to look into how to use the httppostedfile object instead.

Many thanks for your help.

Dave

Jul 19 '06 #6
Hi All

For the benfit of those that come after me, here is the code I used to
get this to work. Basically I use the HTML File control to upload the
selected file into an image object FullSizeImage, then resize that
image as required, then saved the resized image to the server using the
name I want. The redirect command reloads the page, forcing the image
to appear. Hope this helps...

If Me.File1.PostedFile.FileName.Trim.Length = 0 Then
Me.lblUploadError.Text = "A file must be specified before
uploading."
Exit Sub
End If

Try
' Check that the uploaded file is the correct type
If Me.File1.PostedFile.ContentType <"image/pjpeg" And
Me.File1.PostedFile.ContentType <"image/x-png" Then
Me.lblUploadError.Text = "Upload failed - File is not a
recognised JPEG format"
Exit Sub
End If

' Make an image object of the uploaded file using a stream
Dim FullSizeImage As System.Drawing.Image
FullSizeImage =
System.Drawing.Image.FromStream(Me.File1.PostedFil e.InputStream)

' Rotate the image, which should remove any embedded
thumbnails (such as those from digital camera images).
FullSizeImage.RotateFlip(RotateFlipType.Rotate180F lipNone)
FullSizeImage.RotateFlip(RotateFlipType.Rotate180F lipNone)

' The following two lines are necessary for the thumbnail
function to work
Dim DummyCallback As
System.Drawing.Image.GetThumbnailImageAbort
DummyCallback = New
System.Drawing.Image.GetThumbnailImageAbort(Addres sOf DummyFunction)

' Get a resized copy of the image, keeping the proportions
the same.
Dim ResizedImage As System.Drawing.Image
Dim sinWidthReduction As Single = 1
Dim sinHeightReduction As Single = 1
If FullSizeImage.Width 280 Then
' Too wide, so calculate reduction to fit
sinWidthReduction = 280 / FullSizeImage.Width
End If
If FullSizeImage.Height 300 Then
' Too high, so calculate reduction to fit
sinHeightReduction = 300 / FullSizeImage.Height
End If
If sinHeightReduction < 1 Or sinWidthReduction < 1 Then
' Resizing is necessary, so do it
If sinHeightReduction < sinWidthReduction Then
ResizedImage =
FullSizeImage.GetThumbnailImage(FullSizeImage.Widt h *
sinHeightReduction, FullSizeImage.Height * sinHeightReduction,
DummyCallback, IntPtr.Zero)
Else
ResizedImage =
FullSizeImage.GetThumbnailImage(FullSizeImage.Widt h *
sinWidthReduction, FullSizeImage.Height * sinWidthReduction,
DummyCallback, IntPtr.Zero)
End If
Else
ResizedImage = FullSizeImage
End If

' Save the uploaded and resized image to the server
ResizedImage.Save(Server.MapPath("ProductImages/" &
Session("ProductID") & ".jpg"))
Me.lblUploadError.Text = "Upload successful"

' Post back form to force new image to display
Response.Redirect("MaintainProduct.aspx")
Catch ex As Exception
Me.lblUploadError.Text = "Upload failed - " & ex.Message
End Try

Public Function DummyFunction() As Boolean
' This is needed for the image resizing routine above.
Return False
End Function

Dave

Jul 21 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

175
by: Sai Hertz And Control Systems | last post by:
Dear all, Their was a huge rore about MySQL recently for something in java functions now theirs one more http://www.mysql.com/doc/en/News-5.0.x.html Does this concern anyone. What I...
3
by: Lyle Fairfield | last post by:
In a recent thread there has been discussion about Data Access Pages. It has been suggested that they are not permitted on many or most secure sites. Perhaps, that it is so, although I know of no...
0
by: bettervssremoting | last post by:
To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool including SourceOffSite, SourceAnyWhere and VSS Remoting This article makes a detailed...
12
by: Ron Weldy | last post by:
I have a test server runinng 2003/IIS 6 with a mixture of asp and asp.net files. On my workstation I have a share set up to the folder where the web files reside. I am just doing quick and dirty...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.