顯示具有 .net vb 標籤的文章。 顯示所有文章
顯示具有 .net vb 標籤的文章。 顯示所有文章

2022年11月14日 星期一

.NET VB 判斷DBNull的方法

DBNull不等於空值所以要用DBNull去判斷,紀錄一下每次都忘記的DBNull判斷方式


If Not 欄位 Is DBNull.Value Then 

-----

End If


If IsDBNull(欄位) Then

-----

End If

2020年11月17日 星期二

.NET 基礎連接已關閉: 傳送時發生未預期的錯誤。

串接API時一直出現錯誤

基礎連接已關閉: 傳送時發生未預期的錯誤。

上網查找資料後應該是SSL的問題,總之就是串接方已經不接受TLS1.0的協定,所以要強制讓我方以TLS1.1或1.2

需.NET4.0以上,記得先Imports System.Net才能用參數

之後再整個Request前先加上

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

就可以解決了,未來會不會需要更新成更高或其他的協議就未知啦

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
 

2020年3月23日 星期一

.Net VB 圖片上傳後變橫的

爬了下文應該是因為exif資訊的問題,上傳前必須先判斷是否需要轉角度...找了一下總算找到相關內容參考

Public Function TestRotate(sImageFilePath As String) As Boolean
    Dim rft As RotateFlipType = RotateFlipType.RotateNoneFlipNone
    Dim img As Bitmap = Image.FromFile(sImageFilePath)
    Dim properties As PropertyItem() = img.PropertyItems
    Dim bReturn As Boolean = False
    For Each p As PropertyItem In properties
      If p.Id = 274 Then
        Dim orientation As Short = BitConverter.ToInt16(p.Value, 0)
        Select Case orientation
          Case 1
            rft = RotateFlipType.RotateNoneFlipNone
          Case 3
            rft = RotateFlipType.Rotate180FlipNone
          Case 6
           rft = RotateFlipType.Rotate90FlipNone
          Case 8
           rft = RotateFlipType.Rotate270FlipNone
        End Select
      End If
    Next
    If rft <> RotateFlipType.RotateNoneFlipNone Then
      img.RotateFlip(rft)
      System.IO.File.Delete(sImageFilePath)
      img.Save(sImageFilePath, System.Drawing.Imaging.ImageFormat.Jpeg)
      bReturn = True
    End If
    Return bReturn

  End Function

需要import
System.Drawing
System.Drawing.Imaging

RotateFlipType還有很多種,但手機似乎只會遇到以上狀況所以就只用到1、3、6、8

感謝幫助到我的原文,那邊也有C#的寫法

原文網址點我

2019年12月11日 星期三

FileUpload控制項批次上傳範例

找批次上傳的參考時的筆記,紀錄一下,要 .NET版本4.5才能用...


參考網址:
https://dotblogs.com.tw/mis2000lab/2012/04/26/net45_fileupload_allowmultiple



HTML畫面設計:
      注意!!只有一個 FileUpload控制項而已。
                      它的 AllowMultiple屬性已經啟動!



VB語法,範例如下:
    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        '--註解:網站上的目錄路徑。所以不寫磁碟名稱(不寫 “實體”路徑)。
        '--           上傳後的存檔目錄,請依照您的環境作修改。

        Dim saveDir As String = "\[Book]FileUpload\Uploads\"
        Dim appPath As String = Request.PhysicalApplicationPath

        Dim tempfileName As String = Nothing
        Dim myLabel As New System.Text.StringBuilder

        '===========================================
        '== Ony .NET 4.5有這個新的 AllowMultiPle屬性
        '===========================================


        Dim fileName, savePath As String
        For Each postedFile As HttpPostedFile In FileUpload1.PostedFiles
            fileName = postedFile.FileName

            ' –完成檔案上傳的動作。
            savePath = appPath & saveDir & fileName
            postedFile.SaveAs(savePath)

            myLabel.Append("<hr>檔名---- " & fileName)

        Next

        Label1.Text = "上傳成功" & myLabel.ToString()
    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

參考網址: