I'm using vb.net window based and I want to get or download the FileImage in my SQL server. In ASP.NET I can download it via this code. But I want to translate it to windows based. Can you help me please?
Thanks,
Dim oFileType As String = oPortal.GetDs("FileType")
Dim oFile As Byte() = oPortal.GetDs("FileImage")
Dim oRecordKey As String = oPortal.GetDs("FileName")
Response.ContentType = "image/" & oFileType
If Request.QueryString("open") <> "" Then
Response.AppendHeader("Content-Disposition", "attachment; filename=" & oRecordKey)
End If
Response.BinaryWrite(oFile)
Response.End()
Got it. File.WriteAllBytes(oPath & oFileName, WebCtrl.GetDs("FileImage"))
Related
I have an application that i am supporting. I am wondering if it is possible to retrieve an image from a database (Image Datatype) and then attach this image to an email. The application currently embeds the images to an email but i need it to attach images instead.
I have read in to adding attachments to emails but it is grabbing the image froma file location, rather than a database field. I haven't not come across anything that matches my needs.
Do i need to somehow save the image locally then send the email using the saved image?
SQL Server 2014
Pre Existing Get Image Function
Private Function GetImage() As Byte()
Dim rdrImage As SqlDataReader
Dim strSQL As String
Dim arrContent As Byte()
strSQL = "SELECT Photo FROM " & CommonThings1.GetDatabaseSuffix & "Images "
strSQL = strSQL & "WHERE PhotoID = " & Request("ZQ") & " AND Random = " & Request("WXP")
cmCM.CommandText = strSQL
rdrImage = cmCM.ExecuteReader
rdrImage.Read()
arrContent = CType(rdrImage("Photo"), Byte())
rdrImage.Close()
Return arrContent
End Function
Help is greatly appreciated.
Should go something like this, writing this off the top of my head so not tested ...
Dim stream as new MemoryStream(GetImage())
stream.Position = 0 'If I remember correctly, need to be at beginning of stream to read from first position when attaching
Dim MSG As New MailMessage
Dim ToAddress = New MailAddress(SomeEmailAddress)
Dim FromAddress As New MailAddress("SomeEmail#AnEmail.com")
Try
With MSG
.To.Add(ToAddress)
.From = FromAddress
.Subject = "Your Photo"
.IsBodyHtml = True
.Body = SomeHTML
.Attachments.Add(New Attachment(stream , "Photo.jpg", "application/jpg"))
End With
Dim SendMessage As New SmtpClient("smtp.MyMailServer.com")
SendMessage.Send(MSG)
I have a problem with my FTP upload and I hope you can help me. I'm trying create folders and then upload files to them. What my program should do is checking if a folder already exists, and if not, create a new one with the name checked before. The program runs fine, except for the error described below.
My Problem:
I want to upload a folder called ghandle -> works as intended.
After that, I want to upload a folder called handle -> doesn't work, because the .Contains method that checks the folders on the FTP server, finds ghandle and stops, because ghandle contains handle.
Are there other options like .Contains which will just check for whole words or exact matches?
Here is my source code:
Dim dirname = Path.GetFileNameWithoutExtension(openFileDialogHtml.FileName) & "_files"
Dim ftp = "ftp://" & ftp_address.Text & "/"
Dim user = ftp_user.Text
Dim pass = ftp_password.Text
Dim request As Net.FtpWebRequest = Net.FtpWebRequest.Create(ftp)
Dim creds As Net.NetworkCredential = New Net.NetworkCredential(user, pass)
request.Credentials = creds
Dim resp As Net.FtpWebResponse = Nothing
request.Method = Net.WebRequestMethods.Ftp.ListDirectoryDetails
request.KeepAlive = True
Using resp
resp = request.GetResponse()
Dim sr As StreamReader = New StreamReader(resp.GetResponseStream(), System.Text.Encoding.ASCII)
Dim s As String = sr.ReadToEnd()
If Not s.Contains(dirname) Then
request = Net.FtpWebRequest.Create(ftp & dirname)
request.Credentials = creds
request.Method = Net.WebRequestMethods.Ftp.MakeDirectory
resp = request.GetResponse()
MsgBox("Created folder " & dirname)
Else
MsgBox("Folder " & dirname & " already exists!")
End If
End Using
Thanks in advance
First, use ListDirectory, not ListDirectoryDetails. The ListDirectory returns plain names only, what is enough for your purpose and easy to parse.
Then just split the output to an array of individual file names, using the String.Split method:
Dim names As String() =
sr.ReadToEnd().Split(
New Char() {vbCr, vbLf}, StringSplitOptions.RemoveEmptyEntries)
And use the IEnumerable.Contains extension method to check for given file name:
If Not names.Contains(dirname) Then
I have a fully functional "app" I created in Microsoft Access that I use control my Philips Hue lights. They operate via a RESTful interface using JSON commands and it was pretty simple to create the code in VBA. I wanted to make a standalone Windows app though so I can run it on my computers that don't have Access.
I'm trying to use Visual Studio 2015 to make a universal app using VB.net but I'm having problems converting some of my code over. I was able to fix most of the glitches, but I can't get the winHttpReq commands to work. In my research, it sounds like they don't have a direct correlation in VB.net but none of the suggestions I found have worked.
Dim Result As String
Dim MyURL As String, postData As String, strQuote As String
Dim winHttpReq As Object
winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
'Create address and lamp
MyURL = "http://" & IP.Text & "/api/" & Hex.Text & "/lights/" & "1" & "/state"
postData = Code.Text
winHttpReq.Open("PUT", MyURL, False)
winHttpReq.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
winHttpReq.Send(postData)
I get the error that 'CreateObject' is not declared. It may be inaccessible due to its protection level. I'm pretty new to VB.net coding but all the recommendations for alternative posting methods don't seem to work. Any suggestions would be greatly appreciated.
In VB.Net, use WebRequest:
'Create address and lamp
MyURL = "http://" & IP.Text & "/api/" & Hex.Text & "/lights/" & "1" & "/state"
Dim request As WebRequest = WebRequest.Create(MyURL)
' Get the response.
Dim response As WebResponse = request.GetResponse()
' Display the status.
Console.WriteLine(CType(response,HttpWebResponse).StatusDescription)
' Get the stream containing content returned by the server.
Dim dataStream As Stream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
' Display the content.
Console.WriteLine(responseFromServer)
' Clean up the streams and the response.
reader.Close()
response.Close()
I am writing the code using vb.net for file transfer from remote machine to local machine with out using any third party tools
This my code
Dim reqFTP As FtpWebRequest
Dim filepath As String
Dim filename As String
Dim filename1 As String
Dim ftpserverip As String
Dim ftpuserid As String
Dim ftpPassword As String
Try
filename1 = TxtRemoteFile.Text
filepath = TxtLocalFile.Text
filename = Locfname.Text
ftpserverip = TxtServerIP.Text
ftpuserid = TxtUserName.Text
ftpPassword = TxtPwd.Text
Dim outputStream As FileStream = New FileStream((filepath + ("\\" + filename)), FileMode.Create)
reqFTP = CType(FtpWebRequest.Create(New Uri(("ftp://" _
+ (ftpserverip + ("/" + filename1))))), FtpWebRequest)
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile
reqFTP.UseBinary = True
reqFTP.Credentials = New NetworkCredential(ftpuserid, ftpPassword)
Dim response As FtpWebResponse = CType(reqFTP.GetResponse, FtpWebResponse)
outputStream.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
but am getting error like" remote server returned error :(550) fi
I had the same issue. I was not including httpdocs in the remote path.
Example:
ftp://ftp.websitename.com/httpdocs/filenametocopy.txt
System.Net.WebRequest.Create("ftp://ftp.websitename.com/httpdocs/filenametocopy.txt")
Permission was denied because i was trying to write the file outside the root directory.
I am trying to upload a file to Mainframe machine from my VB.net application. I am getting the following error.
The remote server returned an error: (501) Syntax error in parameters or arguments
Below is my code. Any help will be appreciated.
Dim urlHost As String = "ftp://" + FWFTP.Default.TargetFTPHost.ToString()
Dim url As String = "ftp://" + FWFTP.Default.TargetFTPHost & "/" & FWFTP.Default.TargetFolder & "/" & fileName
Dim clsRequest As System.Net.FtpWebRequest = CType(WebRequest.Create(url), FtpWebRequest)
clsRequest.Credentials = New System.Net.NetworkCredential(FWFTP.Default.FTPUserName.ToString(), FWFTP.Default.FTPPassword.ToString())
clsRequest.Proxy = Nothing
Dim target As New Uri(urlHost)
Dim iphe() As System.Net.IPAddress = System.Net.Dns.GetHostAddresses(target.Host)
If My.Computer.Network.Ping(iphe(0).ToString, 200) = False Then
MsgBox("Ping: " + urlHost + ":" + iphe(0).ToString + " Failed")
'ElseIf FTPSite <> "" Then
End If
clsRequest.KeepAlive = False
clsRequest.UseBinary = False
clsRequest.UsePassive = True
clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
' read in file...
Dim bFile() As Byte = System.IO.File.ReadAllBytes(fullPath)
' upload file...
Dim clsStream As System.IO.Stream = _
clsRequest.GetRequestStream()
clsStream.Write(bFile, 0, bFile.Length)
clsStream.Close()
clsStream.Dispose()
When you say 'mainframe', it wouldn't be AS/400 by any chance?
http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/6c621557-62af-4d4a-9afd-e5b127c380f4
In particular, see the suggestion by FTPOdyssey in this thread
When you use System.Net.WebClient or FtpWebRequest and you access a Z/Os mainframe if you want to access the following ftp url (notice the # in the password):
ftp://userid:p#ssword#host/pub/data/msg.log
You must do the following:
ftp://userid:%40ssword#host/%2Fpub/data/msg.log
This should work with either download or upload.