Parameter is not valid error when creating new bitmap - asp.net

I am displaying images on my page and then attempting to find the current height of the images in order to vertically position them. I am doing this by creating a new bitmap and then getting the height of the bitmap, as follows:
Dim sampleImg As New System.Drawing.Bitmap(Server.MapPath(String.Format("/Uploads/Products/Images/w100h100/{0}", e.Item.DataItem("Filename"))))
Dim imgHeight As Integer = sampleImg.Height
When I run this on my local machine, all works fine. However, I have recently uploaded the site so far to my development server and when I run the same code from there, I get this error message:
Parameter is not valid
I have been searching forums to try to find a solution but I have ended up a bit unsure where else to look because the file definitely exists on the development server and the code works fine on my local machine. The server I am using is a VPS and I am quite a beginner with working with servers so I don't know if there is anything I haven't set up on there which is stopping it from working?
Whilst trying to find a solution, I have also tried the following code but I get a System.IO.FileNotFoundException error message:
Dim sampleImg As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath(String.Format("/Uploads/Products/Images/w100h100/{0}", e.Item.DataItem("Filename"))))
I am wondering if anyone can help me find a solution to this, or alternatively is there a different method to acheiving the same end?
All help is very much appreciated. Thanks.

It may be a file permissions issue, you need to ensure that the IUSR account for your site has read access to the image file. To do this -
R-click the file, Properties, Select the "Security" tab - you will have a list of users who have access to the file. A quick check is to add "Everyone" with read priviliges. To keep it locked to your ASP.Net instance only you should start IIS Manager, r-click your app pool and the Identity is the user your app runs under.

If you are running through IIS it may be that the file type isn't set up in the available MIME types for the site.
http://technet.microsoft.com/en-us/library/bb742440.aspx
With IIS7 there is a module for each Site called "MIME Types". Check whether the File Extension is in the list, and if not, add it with the appropriate MIME Type (list of mime types in link above)

I think you have to use ~ root operator to resolve the path issue.
Dim sampleImg As System.Drawing.Image =
System.Drawing.Image.FromFile(Server.MapPath(
String.Format("~/Uploads/Products/Images/w100h100/{0}",
e.Item.DataItem("Filename"))))
OR the specified file is not a valid image file.

Related

Files on network drive are not accessible when website is published on IIS

I have a website that shows a list of recommended documents retrieved from a network drive, Z drive. for each document, if the user pushes a button, he can navigate to the path of the file through windows explorer. below is the code.
string argument = #"/select, " + filePath;
System.Diagnostics.Process.Start("explorer.exe", argument);
an example of filepath is : z:\ecomarathon2012\04-Reports\fuel cell and batteries comparative.docx
Now, I have published the website on IIS but the button does not work anymore to show me the path of the file.
What changes I need to add in order to solve this problem for the published version of the website?
Sounds like your IIS user identity doesn't have access to the network drive. Give that user read access and I believe your code should work. If not, try to log some errors using try catch and post them here.

Get file path & filename with asp:FileUpload, don't want file... just path and name

I am developing a .NET intranet site which will enable the user to see a list of files (file details stored in DB) and link to the actual PDF/XML/XLS and open it... kind of like a table of contents for the network.
During data entry, the user enters various data about a document, then browses to the file on the network and selects it using the asp:FileUpload. The codebehind then saves the network path to the DB. There is alot of overhead here because i'm sending the file to the server but never use it.
Everything has been working fine until someone tries to use a large PDF file then I get the dreaded MAXIMUM REQUEST LENGTH EXCEEDED error... So I'm trying to find a solution here... I do not need the actual file.. just the path and filename.
I know not all browsers send the full path but our systems have older browsers so everything is working fine now, but will probably break soon.. which is another reason to find a different solution.
I've looked into Javascript to pull the path but that won't work...
Any other ideas? Other ways to just grab the path and filename? (besides manually typing it in to a Text field)
Thanks,
Todd.
This may help too
How to get the full path of a file from asp: file upload?
string filename = Path.GetFileName(FileUpload1.FileName);//file name
string path= Server.MapPath(filename);//path

ASP.NET Generating .pdf files, UnauthorizedAccessException on Save()

I have a list of jobs and i give users the option to get job description in form of a pdf file. I use MigraDoc/PDFsharp to generate pdf files.
The problem is, after i render the pdf document and want to save it somewhere on the DevServer i get UnauthorizedAccessException on creating FileStream in PDFsharp PdfDocument.Save() method.
I never really used Windows for anything more advanced than playing games and i'm not sure why would i get this exception since i'm logged in as Administrator user and i guess that my ASP.NET application is running with Administrator privilleges and should be able to write files pretty much anywhere on filesystem.
The Code.
GridViewRow jobRow = (GridViewRow)(sender as Control).Parent.Parent;
Document jobDocument = new Document();
Section xyz = jobDocument.AddSection();
xyz.AddParagraph("Wonderfull job");
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true);
pdfRenderer.Document = jobDocument;
pdfRenderer.RenderDocument();
string filename = "Job_" + jobRow["3"] + ".pdf"; // Job_[title].pdf
pdfRenderer.PdfDocument.Save(filename);
Last line is the line that causes the exception.
Any suggestions? I'm not an ASP.NET developer and I'm forced to use ASP.NET for my school project so this may be a very simple problem but i really don't know what to do and what to search. Thanks for answering!
Administrator account =! IIS user.
The IIS User needs to have writing privileges, too!
On most machines it is know as "IIS_User" or maybe "network service". First you can grant writing permissions to every User. If this works for you, you know what to do.
First try to set an absolute path as filename, maybe a directory outside of "C:\inetpub\wwwroot"?
Hope this helps!

ImageUrl trying to display an image outside of project root

I am working with two different web sites in asp.net. In the first project i upload some images to a specific folder under the project root and save just the filename in the database, now i am trying to display this images at some page of the second project I know the filename from the database and the image folder as absolute pat but I have not been able to display the image, even thought when looking in firebug the image src is correct src="D:/MyFolder/image.jpg" the image does not display, probably because it is not pointing in the right directory.
I have also tried using Server.MapPath and then my D location but still no success.
I am sure someone has faced the same situation before and was really hoping to get some hint to manage this.
Thank you in advance
I found my solution, strange but i didn't catch it before. Uploaded pictures under a project can always be accessed using the url of the project http://www.yourwebsite.com/images/photo.png now in the second project you can use reference the images using this url and concatenating the file name which i store on database. I think this is the best solution and without changing the code access security which i think can bring other problems with it. Anyway thank you guys.
If you want to display the image that is not in your project (I mean it is present in some other project or some other drive) just create the virtual directory in IIS
Go to "Run", type inetmgr
Right click on your project and add virtual directory
Give alias name and path so that it acts like folder in your project
I don't think you can serve files outside of your application path by default. It's called Code Access Security. You can read up on it here:
http://msdn.microsoft.com/en-us/library/930b76w0.aspx
You can fix this by changing your trust level to High in your web.config:
http://msdn.microsoft.com/en-us/library/tkscy493.aspx
I wouldn't recommend doing this for any site that is externally accessible. In fact, depending on how/where you're hosting your application, this option may be restricted.
You can only "link" to files that exist relative to the same project or are hosted on another site via an absolute URL.
If you want to service files outside the application/website (on disk or in a database) you will need to build a mechanism that gets the file and binary writes it to the browser, setting the MIME type etc. This is best done using an HttpHandler.

Change script execute permissions of a directory in IIS6 dynamically thru ASP

This is kind of an advanced problem, hopefully one of you asp/VB gurus will have a suggestion. I am trying to dynamically set a folder's execute permissions in IIS6 from an asp page. I have given the server full permissions to make the changes necessary. When I run the code I get:
Microsoft VBScript runtime error '800a0046'
Permission denied: 'GetObject'
/learning.asp, line 11
I have a server setup with IIS6 running a couple asp websites. I use a code like this to create a folder called "files"
set fso = Server.CreateObject("Scripting.FileSystemObject")
set folder3 = fso.CreateFolder(Server.MapPath(username & "/files"))
set folder3 = nothing
set fso = nothing
Works fine and creates the files folder just where I want it. However, the new folder has execute permissions set to "scripts and exe's" and I need it to be "never" or false. There is a system object that you can call to make this change, and I even have a piece of code that I wrote after reading another tutorial.
It doesn't work and gives the above error. I think, maybe, I am just leaving something out, but this stuff is a little over my head. Here is what I used, but I don't know the syntax or what exactly to call.
Set root = GetObject("IIS://localhost/" & username)
Set newVDir = root.Create("IIsWebVirtualDir","files")
newVDir.Path = "e:\iis3\server2\ADSI"
newVDir.AccessScript = False
newVDir.SetInfo
I have searched around for 3 days trying to find a solution to this, but not very many people do this type of thing apparently, because there are no posts about it that I can find.
Anyway, I don't understand what the getobject is supposed to do, and what newVDir.path I am supposed to be using?
Thank you, in advance, for any helpful suggestions you may have.
This article (see http://technet.microsoft.com/en-us/library/bb742439.aspx) explains what the GetObject actually does.
I believe, what happens behind the scene is that a COM object of type "IIS Admin Service" is (needs to be) created to perform your operations. By default, the IIS application pool account is not allowed to create such object. So maybe its worth to check it. Open Control panel->Administrative services, run "Component services". Find "my computer" in the tree and there find "IIS admin service". Check "activation" and "launch" permissions, add some if necessary.

Resources