Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Code Support: ASP.NET (Non-Forum Related)
 Access denied error with upload script
 New Topic  Topic Locked
 Printer Friendly
Next Page
Author Previous Topic Topic Next Topic
Page: of 2

Davio
Development Team Member

Jamaica
12212 Posts

Posted - 15 April 2006 :  15:08:56  Show Profile
Trying to get Dayve's Photo album script working but running into permission problems.

The error is as follows:

Access to the path "D:\InetPub\wwwroot\files\~images\04152006082631_babyontoilet.jpg" is denied.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:
System.UnauthorizedAccessException: Access to the path "D:\InetPub\wwwroot\files\~images\04152006082631_babyontoilet.jpg" is denied.

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

To grant ASP.NET write access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

Source Error:
Line 37:        Int32 IntFileWidth = UploadedImage.Width;
Line 38:       if (PhotoUpload.PostedFile.FileName != "") 
Line 39: 	    PhotoUpload.PostedFile.SaveAs(FilePath + UploadedFileName);
Line 40:       {
Line 41: 	  if ((UploadedFile.IndexOf(".jpg") > -1)) {
Source File: D:\InetPub\wwwroot\pop_upload.aspx Line: 39

Stack Trace:
[UnauthorizedAccessException: Access to the path "D:\InetPub\wwwroot\files\~images\04152006093341_babyontoilet.jpg" is denied.]
   System.IO.__Error.WinIOError(Int32 errorCode, String str) +393
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, +
      Int32 bufferSize, Boolean useAsync, String msgPath, Boolean bFromProxy) +888
   System.IO.FileStream..ctor(String path, FileMode mode) +52
   System.Web.HttpPostedFile.SaveAs(String filename) +48
   ASP.pop_upload_aspx.UploadBut_Click(Object sender, EventArgs args) in D:\InetPub\wwwroot\pop_upload.aspx:39
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain() +1292



I have went in to Windows Explorer on the server and given the FILES folder Write permission for the IUSER_MachineName account. Refreshed the page, tried again. Still no luck. There was no ASP.NET account listed in the security tab for the FILES folder so I added the ASP.NET account and gave it Read/Write permission. Still no luck. I made sure the permissions propagate to the sub-folders, still no luck.

Anyone can help me with this?

Support Snitz Forums

HuwR
Forum Admin

United Kingdom
20550 Posts

Posted - 15 April 2006 :  15:17:11  Show Profile  Visit HuwR's Homepage
just checking, but did you also give them modify permissions ?
Go to Top of Page

Davio
Development Team Member

Jamaica
12212 Posts

Posted - 15 April 2006 :  15:18:53  Show Profile
Do I need to? Thought the write permission would be enough. I'll try that.

Support Snitz Forums
Go to Top of Page

Davio
Development Team Member

Jamaica
12212 Posts

Posted - 15 April 2006 :  15:30:13  Show Profile
Got the same error. The server doesn't need to be restarted for this, does it?

This is the first time ASP.NET scripts have been running on the server. It works ok when reading aspx scripts, but this one requires uploading files to the server.

Using .NET Framework version 1.1.4322.2300 on IIS 6 on a Windows 2003 server.

Support Snitz Forums
Go to Top of Page

Davio
Development Team Member

Jamaica
12212 Posts

Posted - 15 April 2006 :  15:39:11  Show Profile
Checking the permissions on the wwwroot folder, I notice some Special Permissions for the MachineName\USERS. It has Denied set for "Create Files / Write Data" and "Create Folder / Append Data" options. That shouldn't affect this would it?

Support Snitz Forums
Go to Top of Page

Davio
Development Team Member

Jamaica
12212 Posts

Posted - 17 April 2006 :  16:59:31  Show Profile
Uhg! There was a DENY permissions set on the drive for the USERS member, which was the cause of this. I removed those permissions and the script now works. Took me a while!

Support Snitz Forums
Go to Top of Page

dayve
Forum Moderator

USA
5820 Posts

Posted - 18 April 2006 :  12:08:49  Show Profile  Visit dayve's Homepage
sorry I did not get back to you earlier... I've been on vacation and haven't checked my emails in over a week. glad to see you got it running. I have enhanced the feature quite a bit and still find areas where there can be improvement. feel free to ask any other questions you may have, or offer suggestions on how to improve it. it really has been one of the best additions to my forum.

Go to Top of Page

Davio
Development Team Member

Jamaica
12212 Posts

Posted - 18 April 2006 :  13:23:36  Show Profile
I have converted the script to work with a MySQL database. So I had to get my head into learning some C# syntax and all. But it wasn't that hard. If you want the changes, let me know.

And I wouldn't mind if you shared your enhancements.

Support Snitz Forums
Go to Top of Page

Torborg
Junior Member

Norway
108 Posts

Posted - 16 August 2007 :  08:07:03  Show Profile
I use the same photalbum-mod as described above (and davio has helped me earlier). When I try to upload a picture, I get the error below. Anyone who knows what to do?

Server Error in '/bio/enbiolog' Application.
--------------------------------------------------------------------------------

Syntax error in date in query expression '#16.08.2007 14:04:42#'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: Syntax error in date in query expression '#16.08.2007 14:04:42#'.

Source Error:


Line 176: cmdImgIns = new OleDbCommand(strSQL, conImages);
Line 177: conImages.Open();
Line 178: cmdImgIns.ExecuteNonQuery();
Line 179: cmdImgIns.CommandText="SELECT @@IDENTITY As 'Identity';";
Line 180: OleDbDataReader reader = cmdImgIns.ExecuteReader();


Source File: f:\ekanal\www1\bio\enbiolog\testforum\pop_upload.aspx Line: 178

Stack Trace:


[OleDbException (0x80040e07): Syntax error in date in query expression '#16.08.2007 14:04:42#'.]
System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) +267
System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) +192
System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) +48
System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +106
System.Data.OleDb.OleDbCommand.ExecuteNonQuery() +108
ASP.testforum_pop_upload_aspx.SendToDB(String MemberID, String FilePath, String UploadedFileName, String UploadedDesc, Int32 IntFileSize, Int32 IntFileHeight, Int32 IntFileWidth, String FileType) in f:\ekanal\www1\bio\enbiolog\testforum\pop_upload.aspx:178
ASP.testforum_pop_upload_aspx.UploadBut_Click(Object sender, EventArgs args) in f:\ekanal\www1\bio\enbiolog\testforum\pop_upload.aspx:44
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102

Edited by - Torborg on 16 August 2007 08:11:21
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 16 August 2007 :  09:19:26  Show Profile  Send ruirib a Yahoo! Message
What database are you using? Did the mod work previously? If so, what caused the error to show?


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Torborg
Junior Member

Norway
108 Posts

Posted - 16 August 2007 :  09:43:33  Show Profile
Access database. The forum and the script worked on a different server (kiwihosting). Then i moved it to this Norwegian server and it doesnt work. It is this pop_upload.aspx that failes. Maybe it is some problem with tha date format?

http://www.bio.no/enbiolog/testforum/

Edited by - Torborg on 16 August 2007 09:46:26
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 16 August 2007 :  09:47:42  Show Profile  Send ruirib a Yahoo! Message
It's definitely a problem with the date format. What problem, I cant tell, cause I don't know the code.


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Torborg
Junior Member

Norway
108 Posts

Posted - 16 August 2007 :  09:50:08  Show Profile
I hope it is OK to post the script?. I can delete later if it is not OK.

<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.OleDb" %>
<%@ Import namespace="System.Data.SqlClient" %>
<%@ Page Language="C#" Debug="true"%>
<%@ import namespace="System.IO" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<%@ import Namespace="System.Drawing" %>

<script runat="server">
// Eclectic Networks Image Uploader/Resampler 1.03 (BETA)
// David J. Harris
// Revision Date: December 12, 2004

void UploadBut_Click(Object sender, EventArgs args)
{
DateTime MyDate = DateTime.Now;
String UploadedFile = PhotoUpload.PostedFile.FileName.ToLower();
String UploadedDesc = PhotoDesc.Text;
String FileType = PhotoUpload.PostedFile.ContentType;
int IntFileSize = PhotoUpload.PostedFile.ContentLength;
String MemberID = Request.QueryString["mID"];
String FilePath = Request.PhysicalApplicationPath + "\\testforum\\files\\~images\\";
String t_FilePath = Request.PhysicalApplicationPath + "\\testforum\\files\\~thumbs\\";
int ExtractPos = UploadedFile.LastIndexOf("\\") + 1;
String UploadedFileName = MyDate.ToString("MMddyyyyhhmmss") + "_" + UploadedFile.Substring(ExtractPos,UploadedFile.Length - ExtractPos);
String ImageName = UploadedFileName;

ImageName = Regex.Replace(ImageName, @"\.jpg", "", RegexOptions.IgnoreCase);
ImageName = Regex.Replace(ImageName, @"\.gif", "", RegexOptions.IgnoreCase);
ImageName = Regex.Replace(ImageName, @"\.jpeg", "", RegexOptions.IgnoreCase);
ImageName = Regex.Replace(ImageName, @"\.png", "", RegexOptions.IgnoreCase);
UploadedDesc = Regex.Replace(UploadedDesc, @"""", "", RegexOptions.IgnoreCase);
UploadedDesc = Regex.Replace(UploadedDesc, @"'", "", RegexOptions.IgnoreCase);

Stream ImageStream = PhotoUpload.PostedFile.InputStream;
System.Drawing.Image UploadedImage = System.Drawing.Image.FromStream(ImageStream);
Int32 IntFileHeight = UploadedImage.Height;
Int32 IntFileWidth = UploadedImage.Width;
if (PhotoUpload.PostedFile.FileName != "")
PhotoUpload.PostedFile.SaveAs(FilePath + UploadedFileName);
{
if ((UploadedFile.IndexOf(".jpg") > -1)) {
SaveUploadImageAsJpg(ImageStream, t_FilePath, ImageName);
SendToDB(MemberID, FilePath, UploadedFileName, UploadedDesc, IntFileSize, IntFileHeight, IntFileWidth, FileType);
}
if ((UploadedFile.IndexOf(".jpeg") > -1)) {
SaveUploadImageAsJpeg(ImageStream, t_FilePath, ImageName);
SendToDB(MemberID, FilePath, UploadedFileName, UploadedDesc, IntFileSize, IntFileHeight, IntFileWidth, FileType);
}
if ((UploadedFile.IndexOf(".gif") > -1)) {
SaveUploadImageAsGif(ImageStream, t_FilePath, ImageName);
SendToDB(MemberID, FilePath, UploadedFileName, UploadedDesc, IntFileSize, IntFileHeight, IntFileWidth, FileType);
}
if ((UploadedFile.IndexOf(".png") > -1)) {
SaveUploadImageAsPng(ImageStream, t_FilePath, ImageName);
SendToDB(MemberID, FilePath, UploadedFileName, UploadedDesc, IntFileSize, IntFileHeight, IntFileWidth, FileType);
}
}
}

void SaveUploadImageAsJpg(Stream ImageStream, String FilePath, String ImageName)
{
String Extension = ".jpg";
String Prefix = "thumb_";
String MimeType = "image/jpeg";
long Quality = 100;
UploadImage(ImageStream, FilePath, Prefix + ImageName, Extension, MimeType, Quality, 200, 200);
}

void SaveUploadImageAsJpeg(Stream ImageStream, String FilePath, String ImageName)
{
String Extension = ".jpeg";
String Prefix = "thumb_";
String MimeType = "image/jpeg";
long Quality = 100;
UploadImage(ImageStream, FilePath, Prefix + ImageName, Extension, MimeType, Quality, 200, 200);
}

void SaveUploadImageAsGif(Stream ImageStream, String FilePath, String ImageName)
{
String Extension = ".gif";
String Prefix = "thumb_";
String MimeType = "image/gif";
long Quality = 100;
UploadImage(ImageStream, FilePath, Prefix + ImageName, Extension, MimeType, Quality, 200, 200);
}

void SaveUploadImageAsPng(Stream ImageStream, String FilePath, String ImageName)
{
String Extension = ".png";
String Prefix = "thumb_";
String MimeType = "image/png";
long Quality = 0; // has no effect on png files
UploadImage(ImageStream, FilePath, Prefix + ImageName, Extension, MimeType, Quality, 200, 200);
}

void UploadImage(Stream ImageStream, String FilePath, String ImageName, String Extension, String MimeType, long Quality, int maxWidth, int maxHeight)
{
try
{
System.Drawing.Image UploadedImage = System.Drawing.Image.FromStream(ImageStream);

int intNewWidth,intNewHeight;

Int32 old_height = UploadedImage.Height;
Int32 old_width = UploadedImage.Width;

if (maxWidth < old_width || maxHeight < old_height) {
if (maxWidth >= maxHeight) {
intNewWidth = (int)((double)maxHeight*((double)old_width/(double)old_height));
intNewHeight = maxHeight;
} else {
intNewWidth = maxWidth;
intNewHeight = (int)((double)maxWidth*((double)old_height/(double)old_width));
}
if (intNewWidth > maxWidth) {
intNewWidth = maxWidth;
intNewHeight = (int)((double)maxWidth*((double)old_height/(double)old_width));
}
if (intNewHeight > maxHeight) {
intNewWidth = (int)((double)maxHeight*((double)old_width/(double)old_height));
intNewHeight = maxHeight;
}
} else {
intNewWidth = old_width;
intNewHeight = old_height;
}

Bitmap smallerImg = new Bitmap(intNewWidth, intNewHeight);
Graphics g = Graphics.FromImage(smallerImg);
g.DrawImage(UploadedImage, 0, 0, intNewWidth, intNewHeight);

UploadedImage.Dispose();

if ( !Extension.Equals(".png") )
{
EncoderParameters myParams = new EncoderParameters(1);
myParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Quality );
smallerImg.Save( FilePath + ImageName + Extension, GetEncoderInfo(MimeType), myParams);
}
else
{
smallerImg.Save( FilePath + ImageName + Extension , ImageFormat.Png);
}
smallerImg.Dispose();
}
finally { }
}

private ImageCodecInfo GetEncoderInfo(string mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for(j = 0; j < encoders.Length; ++j)
{
if(encoders[j].MimeType == mimeType)
{
return encoders[j];
}
}
return null;
}

void SendToDB(String MemberID, String FilePath, String UploadedFileName, String UploadedDesc, int IntFileSize, int IntFileHeight, int IntFileWidth, String FileType)
{
OleDbConnection conImages;
OleDbCommand cmdImgIns;
string myConnString;
string strSQL;

myConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath ("/enbiolog/fpdb/tenbiolog.mdb");
//myConnString = "Provider=SQLOLEDB;Data Source=DB_SERVER; Initial Catalog=DATABASE_NAME;User Id=USER_ID;Password=PASSWORD; Connect Timeout=15;Network Library=dbmssocn;";
strSQL = "INSERT INTO FORUM_IMAGES (I_LOC, I_DESC, I_MID, I_DATE, I_SIZE, I_HEIGHT, I_WIDTH, I_TYPE) VALUES ('" + UploadedFileName + "', '" + UploadedDesc + "', " + MemberID + ", #" + DateTime.Now + "#, " + IntFileSize + ", " + IntFileHeight + ", " + IntFileWidth + ", '" + FileType + "');";
conImages = new OleDbConnection(myConnString);
cmdImgIns = new OleDbCommand(strSQL, conImages);
conImages.Open();
cmdImgIns.ExecuteNonQuery();
cmdImgIns.CommandText="SELECT @@IDENTITY As 'Identity';";
OleDbDataReader reader = cmdImgIns.ExecuteReader();
reader.Read();
int id = Int32.Parse(reader.GetValue(0).ToString());
reader.Close();
conImages.Close();

String myScript;
myScript = "<script>window.opener.document.PostTopic.Message.value+= ' [image=" + id + "] ';self.close();<"+"/script>";
Page.RegisterStartupScript("ClientScript", myScript);
}
</script>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Attach Image</title>
<style>
body {font-family:Verdana, Arial, Helvetica; font-size:12px; color:black; background-color:#FFFFFF; margin:2px; margin-top:15px;}
</style>
</head>

<body>
<form id="MainForm" enctype="multipart/form-data" runat="server">
<table border="0" cellpadding="0" align="center" valign="center">
<tr>
<td>Foto:</td>
</tr>
<tr>
<td style="padding-bottom:2px;"><input id="PhotoUpload" runat="server" type="File" size="30"></td>
</tr>
<tr>
<td>Beskrivelse:</td>
</tr>
<tr>
<td style="padding-bottom:2px;"><asp:textbox id="PhotoDesc" size="30" maxlength="50" runat="server"/></td>
</tr>
<tr>
<td style="padding-bottom:2px;" align="center">
<asp:button id="AddSaveButton" text="Klikk for ĺ laste inn" onclick="UploadBut_Click" runat="server" />
</td>
</tr>
</table>
</form>
</body>
</html>

Edited by - Torborg on 16 August 2007 09:50:36
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20550 Posts

Posted - 16 August 2007 :  10:26:58  Show Profile  Visit HuwR's Homepage
you should be able to fix it by giving the date a different format, look for this DateTime.Now in the SQL statement and change it to something like

DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss")
Go to Top of Page

Torborg
Junior Member

Norway
108 Posts

Posted - 16 August 2007 :  12:52:31  Show Profile
Thanks Huw and Rui. I think Huws answer solved my problem.
But now I got a new error, same file:

Server Error in '/bio/enbiolog' Application.
--------------------------------------------------------------------------------

The given path's format is not supported.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NotSupportedException: The given path's format is not supported.

Source Error:


Line 38: Int32 IntFileWidth = UploadedImage.Width;
Line 39: if (PhotoUpload.PostedFile.FileName != "")
Line 40: PhotoUpload.PostedFile.SaveAs(FilePath + UploadedFileName);
Line 41: {
Line 42: if ((UploadedFile.IndexOf(".jpg") > -1)) {


Source File: f:\ekanal\www1\bio\enbiolog\testforum\pop_upload.aspx Line: 40

Stack Trace:


[NotSupportedException: The given path's format is not supported.]
System.Security.Util.StringExpressionSet.CanonicalizePath(String path, Boolean needFullPath) +2726486
System.Security.Util.StringExpressionSet.CreateListFromExpressions(String[] str, Boolean needFullPath) +331
System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList) +105
System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, AccessControlActions control, String[] pathList, Boolean checkForDuplicates, Boolean needFullPath) +54
System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) +671
System.IO.FileStream..ctor(String path, FileMode mode) +65
System.Web.HttpPostedFile.SaveAs(String filename) +87
ASP.testforum_pop_upload_aspx.UploadBut_Click(Object sender, EventArgs args) in f:\ekanal\www1\bio\enbiolog\testforum\pop_upload.aspx:40
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 16 August 2007 :  13:27:50  Show Profile  Send ruirib a Yahoo! Message
Try adding :

Response.Write (FilePath + UploadedFileName);
Response.End

just before line 40;

Let's see if this shows the path and filename name being used.


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page
Page: of 2 Previous Topic Topic Next Topic  
Next Page
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2019 Snitz™ Communications Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000 Version 3.4.07