Ajax Control Toolkit htmleditorextender AjaxFileUpload Path - asp.net

I'm trying to save an image to a local path (I'm not going to be deploying my project on a server) and I'm having difficulty with putting my finger on the correct path for the file upload:
Protected Sub htmleditorextender_ImageUploadComplete(sender As Object, e As AjaxControlToolkit.AjaxFileUploadEventArgs)
Dim fullpath = Request.MapPath("~/img/ar") & "/" & e.FileName
TextBox1_HtmlEditorExtender.AjaxFileUpload.SaveAs(fullpath)
e.PostedUrl = fullpath
End Sub
This doesn't work, and no image is uploaded to the said folder. What's going wrong?

try this
Protected Sub HTMLEditorExtender_ImageUploadComplete(sender As Object, e As AjaxControlToolkit.AjaxFileUploadEventArgs)
Dim fullpath As String = Server.MapPath("~/img/ar/") + e.FileName
HTMLEditorExtender.AjaxFileUpload.SaveAs(fullpath)
e.PostedUrl = Page.ResolveUrl(fullpath)
End Sub

Related

The request is not supported. Encrypting an Image in ASP.net

I am trying to create a simple encryption program with visual basic on visual studio. My program is to encrypt an image then decrypt it. The system is saying that the request is not supported. As a note I am just learning about encrypting and not sure if I am even doing this correctly. Any comments or help would be much appreciated.
Error is from this: File.Encrypt(FileName)
if my encrypt is creating an error then my decrypt will most likely as well
Imports System
Imports System.IO
Imports System.Security.Cryptography
Partial Class _Default
Inherits System.Web.UI.Page
'My Encrypt button that takes the file from my FileUpload tool and Encrypts it, then outputs on my label
'that the file was successfully encrypted
Protected Sub EncryptButton_Click(sender As Object, e As EventArgs) Handles EncryptButton.Click
Dim FileName As String = FileUpload1.FileName
File.Encrypt(FileName)
Label1.Text = "Encrypt" + FileName
End Sub
'My Decrypt button that takes the file from my FileUpload tool and Encrypts it, then outputs on my label
'that the file was successfully encrypted
Protected Sub DecryptButton_Click(sender As Object, e As EventArgs) Handles DecryptButton.Click
Dim FileName As String = FileUpload1.FileName
File.Decrypt(FileName)
Label1.Text = "Decrypt" + FileName
End Sub
'Load page that will display a success output on the label if the upload is completed
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If FileUpload1.HasFile = True Then
Label1.Text = "Success"
Else
Label1.Text = "Failed"
End If
End Sub
Protected Sub CustomValidator1_ServerValidate(source As Object, args As ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
'Verify the control has a file
If Not FileUpload1.HasFile Then
CustomValidator1.ErrorMessage = "A file is required in order to proceed"
args.IsValid = False
Else
'next 2 lines are all one line
Dim ext As String =
System.Web.VirtualPathUtility.GetExtension(FileUpload1.FileName).ToUpper()
If Not ext = ".GIF" And Not ext = ".JPG" And Not ext = ".PNG" Then
'next 2 lines are all one line
CustomValidator1.ErrorMessage = String.Concat("Invalid file type '", ext, "' -must be .gif or .jpg or .png to continue")
args.IsValid = False
Else
args.IsValid = True
End If
End If
End Sub
End Class
I searched for "System.IO.IOException" and found Troubleshooting Exceptions: System.IO.IOException which states
Make sure the file and directory exist.
Then looking at the code:
Dim FileName As String = FileUpload1.FileName
I see that there is no directory specified for the file. So, you need to use Path.Combine so that you can give File.Encrypt the full filename including the path.
(There is no indiction in the posted code of which directory the uploaded file is in, so I can't help further with that. It could be that FileUpload1 has a property which gives that.)

How to create a link to a website in vb code behind?

It may be simple but the search words in Google give too many irrelevant results.
Protected Sub Menu1_MenuItemClick(sender As Object, e As MenuEventArgs) Handles Menu1.MenuItemClick
If e.Item.Text = "SomeItem" Then
'The link goes here
End If
End Sub
Use Response.Redirect if you want to send the current page to a new url:
Protected Sub Menu1_MenuItemClick(sender As Object, e As MenuEventArgs) Handles Menu1.MenuItemClick
If e.Item.Text = "SomeItem" Then
Response.Redirect("http://www.stackoverflow.com")
End If
End Sub
To open a new url in a new window/tab you would have to use javascript. Normally I would recommend just putting the javascript directly onto the aspx page but in the event that the url will use data from the code behind to generate the url you can use the ClientScript.RegisterStartupScript function.
Protected Sub Menu1_MenuItemClick(sender As Object, e As MenuEventArgs) Handles Menu1.MenuItemClick
If e.Item.Text = "SomeItem" Then
Dim sURL As String = "http://www.stackoverflow.com"
ClientScript.RegisterStartupScript(Me.GetType(), "script", "window.open('" & sURL + "', 'popup_window');", True)
End If
End Sub

How would I change a property of an image without knowing its ID?

I want to use the Sector variable that is sent to the SetSectorImage sub to name the image that is being changed (the images do already exist on the web form, I am only changing the URLs). A Google search only led me to an article on MSDN about the CallByName method but I don't know if it will work in this situation and of it will, I can't figure out how.
Here's the article if it helps: http://msdn.microsoft.com/en-us/library/22x2chfx.aspx
Imports System
Imports System.IO
Public Class Launcher
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
SetSectorImage("Sector1")
SetSectorImage("Sector2")
SetSectorImage("Sector3")
End Sub
Sub SetSectorImage(Sector As String)
Dim SectorStatus As String
Try
Using Reader As New StreamReader(Sector + ".txt")
SectorStatus = Reader.ReadToEnd()
Reader.Close()
Sector_SHOULD_BE_USED_HERE.ImageUrl = ("~/Images/" + SectorStatus)
End Using
Catch ex As Exception
ErrorMessage.Text = ("There was an error reading the status of: " + Sector)
ErrorMessage.Visible = True
End Try
End Sub
End Class
Let's suppose you have Images on the page:
<asp:Image ID="Sector1" />
<asp:Image ID="Sector2" />
<asp:Image ID="Sector3" />
You can access them with Control.FindControl Method
Dim myControl1 As Control = FindControl(Sector)
Dim myImage As Image = myControl1
myImage.ImageUrl = ("~/Images/" + SectorStatus)
I did some more research on CallByName() and found that I can use it in this situation. The line to change the image URL would be:
CallByName(Sector, "ImageUrl", CallType.Set, "~/Images/" + SectorStatus)

unable to view image using file path

Issue: Unable to display image using FilePath
I have File Upload button which captured the file path & when I click on Upload button, it should be display the image which I have selected. But its not happening?
What I missed here?? do I need to convert the path to image url?? If Yes then please provide me the code behind it??
Thanks in advance..
Here is my code....
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Dim path As String
path = Server.MapPath(FileUpload1.FileName)
Image1.ImageUrl = path
Label1.Text = Image1.ImageUrl
End Sub
You forgot to save the file?
FileUpload1.SaveAs(string SomeFileNameOnServer) should happen too, before you display from the server.
(not sure how the above line is written in VB.NET)
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Dim path As String
path = Server.MapPath("~/.") + FileUpload1.FileName;
FileUpload1.SaveAs(path)
Image1.ImageUrl = path
Label1.Text = Image1.ImageUrl
End Sub

Using and accessing Webresource

I know this has been asked before. Yes, i did my research but it doesn't seem to be working for me so i hope you experts can help me out :)
Here's what my Project looks like
http://i.stack.imgur.com/nnPZJ.png
Yes, the Build Action is Embedded Resource. I also added this in the AssemblyInfo
Assembly: WebResource("WFL.WebResource.EXT.XXX.png", "image/png")
So now, in the default.aspx i say
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim cs As ClientScriptManager = Page.ClientScript
Dim strReturn As String = cs.GetWebResourceUrl(Me.GetType(), "WFL.WebResource.EXT.XXX.png")
Dim strReturn2 As String = cs.GetWebResourceUrl(GetType(_Default), "WFL.WebResource.EXT.XXX.png")
Image1.ImageUrl = "http://localhost" + strReturn
Image2.ImageUrl = "http://localhost" + strReturn2
Response.Write("http://localhost" + strReturn)
Response.Write("http://localhost" + strReturn2)
End Sub
But when accessing the returned URL, i get The resource cannot be found.
What am i doing wrong?
Thanks in advance.
This being VB and not C#, you don't need the .EXT portion in WFL.WebResource.Ext.XXX.png. That is something C# needs (to specify folder paths), but VB doesn't. You just want your namespace then resource. Try WFL.WebResource.XXX.png and see if that works.

Resources