upload / dowload files to azure RRS feed

  • Question

  • User1717218719 posted

    I have the following code which uploads files to the local host then allows user to download the file. This code workes perfectly however what I now want to be able to do is link this to my azure storage account. if a file is uploaded to this website I want it to auto upload to azure. How would I go about altering this code any help is appreciated. Thanks

    Note my data source is a table in Microsoft sql.

    Partial Class _Default
      Inherits System.Web.UI.Page
      Protected Sub btnAddNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
      End Sub
      Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        Select Case (e.Row.RowType)
          Case DataControlRowType.DataRow
            Dim myFileInfo As FileInfo = CType(e.Row.DataItem, FileInfo)
            Select Case myFileInfo.ContentType.ToLower()
              Case "image/pjpeg"          ' .jpg files
              Case "image/gif"            ' .gif files
              Case "application/msword"   ' .doc files
              Case "text/plain"           ' .txt files 
                ' Do nothing. When the row contains a viewable type, 
                ' we want the View link to be enabled.
              Case Else
                ' Find the View link and disable it.
                Dim myLink As HyperLink = CType(e.Row.FindControl("lnkView"), HyperLink)
                myLink.Enabled = False
            End Select
        End Select
      End Sub
    End Class
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title>Uploaded Files</title>
      <form id="form1" runat="server">
          <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="odsFiles" OnRowDataBound="GridView1_RowDataBound">
              <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
              <asp:BoundField DataField="OriginalName" HeaderText="OriginalName" SortExpression="OriginalName" />
              <asp:BoundField DataField="ContentType" HeaderText="ContentType" SortExpression="ContentType" />
              <asp:BoundField DataField="DateCreated" HeaderText="DateCreated" SortExpression="DateCreated" />
              <asp:HyperLinkField DataNavigateUrlFields="Id" DataNavigateUrlFormatString="~/DownloadFile.aspx?Id={0}" HeaderText="Download" Text="Download" />
              <asp:TemplateField HeaderText="View">
                  <asp:HyperLink ID="lnkView" runat="server" NavigateUrl='<%# Eval("Id", "~/ViewFile.aspx?Id={0}") %>' Text="View"></asp:HyperLink>
          <br />
          <br />
          <asp:Button ID="btnAddNew" runat="server" OnClick="btnAddNew_Click" Text="Add New File" /><br />
          <br />
          <br />
          <asp:ObjectDataSource ID="odsFiles" runat="server" SelectMethod="GetList" TypeName="FileInfo" />

    Wednesday, January 15, 2020 9:49 AM


  • User281315223 posted

    Assuming that you already have a Blob Storage Container configured in Azure (with the appropriate connection string), then you can just use a BlobServiceClient to handle uploading your file as a stream to your container:

    // Create a BlobServiceClient object which will be used to create a container client
    BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
    // Read your file (after casting from your example)
    var file = e.Row.DataItem as FileInfo;
    using(FileStream stream = file.OpenRead())
        // Upload your file

    You can find a complete tutorial to handle this here, but hopefully this should get the basic idea. Additionally, if you need to convert between C# and Visual Basic (as most examples will be in C#), you can use this converter here.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 16, 2020 2:13 AM