顯示具有 VB 標籤的文章。 顯示所有文章
顯示具有 VB 標籤的文章。 顯示所有文章

2020年5月8日 星期五

把Base64 轉成 Byte Array 後儲存為圖檔

專案需要將撈出來的HTML頁面產生PDF...慢慢刻太花時間外加CSS很容易跑掉,乾脆轉圖檔儲存,但用html2canvas轉完後是base64,只好轉存成byte再save圖檔給PDF用...紀錄一下參考的base64 to byte文章
 
HTML Markup
The following HTML Markup consists of an ASP.Net FileUpload control to upload the Image File and a Button control to trigger the File upload process.
<form id="form1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload"
    onclick="btnUpload_Click" />
</form>
 
 
Convert Base64 string to Byte Array using C# and VB.Net
When the Upload button is clicked, the Image file is read into a Byte Array using the BinaryReader class object.
The Byte Array is then converted into Base64 encoded string using the Convert.ToBase64String method.
Now in order to save the Base64 encoded string as Image File, the Base64 encoded string is converted back to Byte Array using the Convert.FromBase64String function.
Finally the Byte Array is saved to Folder on Disk as File using WriteAllBytes method of the File class.
C#
protected void btnUpload_Click(object sender, EventArgs e)
{
    //***Convert Image File to Base64 Encoded string***//
 
    //Read the uploaded file using BinaryReader and convert it to Byte Array.
    BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream);
    byte[] bytes = br.ReadBytes((int)FileUpload1.PostedFile.InputStream.Length);
 
    //Convert the Byte Array to Base64 Encoded string.
    string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
 
    //***Save Base64 Encoded string as Image File***//
 
    //Convert Base64 Encoded string to Byte Array.
    byte[] imageBytes = Convert.FromBase64String(base64String);
       
    //Save the Byte Array as Image File.
    string filePath = Server.MapPath("~/Files/" + Path.GetFileName(FileUpload1.PostedFile.FileName));
    File.WriteAllBytes(filePath, imageBytes);
}
 
VB.Net
Protected Sub btnUpload_Click(sender As Object, e As EventArgs)
    '***Convert Image File to Base64 Encoded string***
 
    'Read the uploaded file using BinaryReader and convert it to Byte Array.
    Dim br As New BinaryReader(FileUpload1.PostedFile.InputStream)
    Dim bytes As Byte() = br.ReadBytes(CInt(FileUpload1.PostedFile.InputStream.Length))
 
    'Convert the Byte Array to Base64 Encoded string.
    Dim base64String As String = Convert.ToBase64String(bytes, 0, bytes.Length)
 
    '***Save Base64 Encoded string as Image File***
 
    'Convert Base64 Encoded string to Byte Array.
    Dim imageBytes As Byte() = Convert.FromBase64String(base64String)
 
    'Save the Byte Array as Image File.
    Dim filePath As String = Server.MapPath("~/Files/" + Path.GetFileName(FileUpload1.PostedFile.FileName))
    File.WriteAllBytes(filePath, imageBytes)
End Sub
 

2019年11月24日 星期日

.net vb 排除特定符號字元

需要阻擋透過表單傳遞惡意傳遞sql或其他指令,本想用正則表達式但考慮到email跟一些使用者可能使用的符號所以還是作罷,最後用比對方式處理,紀錄一下參考資料

VB.NET code

Public Class Form3
    Private ReadOnly InvalidChars As String = "[" + System.Text.RegularExpressions.Regex.Replace(""";&^%$#", ".", "\$&") + "]"
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim s As String = "有特殊字符哦"
        If System.Text.RegularExpressions.Regex.Match(s, InvalidChars).Success Then
            MessageBox.Show("有特殊字符")
        Else
            MessageBox.Show("没有特殊字符")
        End If
    End Sub
End Class

參考網址:

2017年12月6日 星期三

[VB]將讀出的資料去除HTML標籤方法

利用客戶輸入的資料來當fb的描述時發現必須把html標籤去除...

網路上找到了個function不錯用,紀錄一下


Public Function splitHTML(ByVal Html As String) As String
    Dim split As String = Html
    Dim Regex As New System.Text.RegularExpressions.Regex("<[^>]+>|]+>")
    split = Regex.Replace(split, "")
    Return split
End Function

來源:https://dotblogs.com.tw/joysdw12/2010/11/11/19379

2014年3月9日 星期日

取月份最後一天

找玩資料發現用不到..XD" 記下來當memo

轉路自十一的.net 部落


'做法都是用隔月的第一天減一天

'VB

Private Sub Command1_Click()
    Dim d As Date
        d = "2006/05/03"
        MsgBox (DateAdd("M", 1, Format(d, "YYYY/MM") & "/01") - 1)
End Sub
'VB.NET

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim d As Date
        d = "2006/05/03"
        MsgBox(Date.Parse(d.AddMonths(1).ToString("yyyy/MM") & "/01").AddDays(-1))

End Sub





'SQL

在SQL 有LASTDATE() 可以用

2012年4月18日 星期三

資料庫比對checkbox是否打勾

後台編輯頁面時用的方法..因為是比對字串..若有類似的字串時..不知道會不會發生誤判..

較安全做法大概還是得把值改成123456...再另外去轉成要的字串值可能可以更精確的比對出結果..

前台html


<input name="Application" id="Application1" type="checkbox" value="車位" />車位
<input name="Application" id="Application2" type="checkbox" value="雅房" />雅房
<input name="Application" id="Application3" type="checkbox" value="套房" />套房
<asp:Literal ID="Literal2" runat="server"></asp:Literal>

為了撈資料方便 直接把JS寫到.VB中...再呼叫出來用

.VB的部分程式碼


Literal2.Text = "<script language='javascript'>" & vbCrLf
Literal2.Text &= "var array ='" & oReader("Application") & "';" & vbCrLf
Literal2.Text &= "for (var i=1;i<=16;i++){" & vbCrLf
Literal2.Text &= "  if(array.indexOf(document.getElementById('Application'+i).value) > -1){" & vbCrLf
Literal2.Text &= "      document.getElementById('Application'+i).checked=true" & vbCrLf
Literal2.Text &= "  }" & vbCrLf
Literal2.Text &= "}" & vbCrLf
Literal2.Text &= "</script>" & vbCrLf

oReader("Application")是撈出資料表的資料再定義字串去比對

因為是checkbox..所以只要資料中有的就是要打勾的了 

所以直接用array.indexOf(document.getElementById('Application'+i).value) > -1 去判斷位置 

只要有資料一定是大於-1