LightSwitch文件的上传,下载及保存(数据库版)

需要在服务器端数据库中建立以下字段

二进制、文件名、扩展名


 Private Sub 上传_Execute()
            ' 在此编写您的代码。
            Dispatchers.Main.Invoke(
               Sub()
                   '注意!!!要新建数据记录并保存后,才能使用上传功能。否则会提示未生成实例错误。
                   Dim openDialog As New Controls.OpenFileDialog
                   openDialog.Filter = "All File|*.*"
                   If openDialog.ShowDialog = True Then
                       Using fileData As System.IO.FileStream = openDialog.File.OpenRead
                           Dim fileLen As Long = fileData.Length
                           If fileLen > 0 Then
                               Dim sizeInMB = fileLen / 1048576
                               If sizeInMB > 2 Then
                                   Me.ShowMessageBox("File Cannot be > 2MB") : Exit Sub
                               End If
                               Dim fileBArray(fileLen - 1) As Byte : fileData.Read(fileBArray, 0, fileLen) : fileData.Close()
                               '把打开的文件存入数据库二进制字段,upfileProperty为屏幕中名称,如为子表或窗口。则为me.表名.selectitem.字段
                               Me.SjWenJianGuanLiSet.SelectedItem.文件二进制 = fileBArray
                               '把打开的文件扩展名存入数据库
                               文件类型 = openDialog.File.Extension.ToString
                               '把打开的文件名存入数据库
                               Me.SjWenJianGuanLiSet.SelectedItem.文件名称 = openDialog.File.Name.ToString
                           End If
                       End Using
                   End If
               End Sub)
        End Sub

***********************************************************************************

 Private Sub 下载_Execute()
            ' 在此编写您的代码。

            Dispatchers.Main.Invoke(
          Sub()
              If Me.SjWenJianGuanLiSet.SelectedItem.文件二进制 Is Nothing Then
                  Me.ShowMessageBox("数据库未上传相关文件!", "提示!", MessageBoxOption.Ok)
                  Exit Sub
              End If
              Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream(Me.SjWenJianGuanLiSet.SelectedItem.文件二进制)
              Dispatchers.Main.Invoke(
                  Sub()
                      Dim saveDialog As New Controls.SaveFileDialog
                      saveDialog.DefaultFileName = Me.SjWenJianGuanLiSet.SelectedItem.文件名称
                      If saveDialog.ShowDialog = True Then
                          Using FileStream As Stream = saveDialog.OpenFile
                              ms.WriteTo(FileStream)
                          End Using
                      End If
                  End Sub)
          End Sub)

        End Sub


***********************************************************************************

Private Sub 打开_Execute()
            ' 在此编写您的代码。
            Try
                If AutomationFactory.IsAvailable Then
                    If Me.SjWenJianGuanLiSet.SelectedItem.文件二进制 Is Nothing Then
                        Me.ShowMessageBox("数据库未上传相关文件!", "提示!", MessageBoxOption.Ok)
                        Exit Sub
                    End If
                    Dim fullFilePath As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Me.SjWenJianGuanLiSet.SelectedItem.文件名称)
                    Dim fileData As Byte() = Me.SjWenJianGuanLiSet.SelectedItem.文件二进制.ToArray
                    If fileData IsNot Nothing Then
                        Using fs As New FileStream(fullFilePath, FileMode.OpenOrCreate, FileAccess.Write)
                            fs.Write(fileData, 0, fileData.Length)
                            fs.Close()
                        End Using
                    End If
                    Dim shell = AutomationFactory.CreateObject("Shell.Application")
                    shell.shellexecute(fullFilePath)
                End If
            Catch ex As Exception
                'Me.ShowMessageBox(ex.ToString)
            End Try
        End Sub


二进制文件大小验证控制:

 

Private Sub 文件二进制_Validate(results As EntityValidationResultsBuilder)
            ' results.AddPropertyError("<错误消息>")
            '服务器端验证文件大小是否超过2MB,会影响性能。如页面中出现验证次数较少的话,建议页面中验证。
            If Me.文件二进制 IsNot Nothing Then
                Dim sizeInMB = Me.文件二进制.Length / 1048576
                If sizeInMB > 2 Then
                    results.AddPropertyError("File Cannot be > 2MB")
                End If
            End If
        End Sub


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
In this fully updated second edition, award-winning author Tim Leung explains how to build data-centric business applications for the desktop, cloud, web, and mobile devices in just a few clicks―with no code required―using Visual Studio Lightswitch 2015. This book explains the basics of Visual Studio Lightswitch 2015 plus new features and key advanced topics that every Microsoft developer needs to know to create modern data services and build clients that can run on multiple devices. Visual Studio LightSwitch 2015 is a rapid application deployment tool that simplifies and shortens the time needed to develop business applications. The basics are very easily understood but more advanced users will hunger for more. How do you design complex layouts? How do you query data using LINQ and other syntax structures? How do you secure your application against malicious use? Visual Studio LightSwitch 2015 answers these questions and more as author Tim Leung―winner of a Microsoft 2011 Community Contributor Award for his LightSwitch expertise―covers this breakthrough product in detail. For serious developers building, enhancing, and deploying advanced business applications using LightSwitch makes sense because they can benefit from the elegance, convenience, and cost savings afforded by rapid application development before going beyond the "click-and-you're-done" interface to include the extra value and depth of coding expertise that their clients value. What You Will Learn: Build Desktop and HTML5 business applications for PC or mobile devices Create compelling user interfaces that can support multiple languages Fine tune your application with C#, VB.NET, JQuery, JavaScript, and CSS code Integrate with mapping, GPS, and location services Provide email notification, and Microsoft Office compatible data exports Enable users to carry out advanced searches on data Build screen controls that you can share with other developers Table of Contents Part I: Introducing LightSwitch Chapter 1: Introducing LightSwitch Chapter 2: Setting Up Your Data Chapter 3: Building HTML Applications Chapter 4: Creating Desktop Applications Part II: Working with Data Chapter 5: Quer ying Your Data Chapter 6: Writing Data-Access Code Chapter 7: Validating Data Part III: Writing ScreenCode Chapter 8: Refinining HTML Clients Screens with JavaScript/CSS Chapter 9: Enhancing Desktop Screens with .NET Code Part IV: Refining Your Application Chapter 10: Searching Data Chapter 11: Building Practical Application Features Chapter 12: Supporting Multiple Languages Chapter 13: Creating and Using Custom Controls Part V: Extending the Reach of Your Data Chapter 14: Optimizing Data Connectivity with RIA Services Chapter 15: Sharing Data with OData Part VI: Getting Data Out Chapter 16: Creating Reports Chapter 17: Generating Office Documents Chapter 18: Sending Email Part VII: Extending LightSwitch Chapter 19: Creating Control Extensions Chapter 20: Creating Data and Presentation Extensions Part VIII: Securing Your Application Chapter 21: Authenticating Your Users Chapter 22: Authorizing Your Users Part IX: Going Live with Your Application Chapter 23: Deploying Applications Appendix A: Culture Names Appendix B: Data Type Identifiers Appendix C: Using Properties in Custom Controls Appendix D: Custom Screen Template View IDs Appendix E: Data Schemas
要通过手机,阿里云物联网平台和树莓派实现远程控制GPIO口,需要具备以下条件: 1. 一台树莓派 2. 一个阿里云账号,并且开通物联网平台服务 3. 一个可以远程访问的网络环境 下面是具体的步骤及代码: 1. 配置树莓派GPIO 在树莓派上使用GPIO控制LED灯,需要先安装RPi.GPIO库。在终端输入以下命令安装: ``` sudo apt-get update sudo apt-get install rpi.gpio ``` 安装完成后,在终端输入以下命令打开GPIO 17口: ``` sudo gpio mode 0 out ``` 2. 创建阿里云物联网平台设备 登录阿里云物联网平台控制台,创建一个新的设备,选择“自定义设备”,填写设备信息。 3. 在设备上安装SDK 在树莓派上使用Python SDK连接阿里云物联网平台,需要先安装SDK。在终端输入以下命令安装: ``` pip install aliyun-python-sdk-core pip install aliyun-python-sdk-iot ``` 4. 编写Python程序 在终端输入以下命令创建一个新的Python文件: ``` nano aliyun-iot.py ``` 将以下代码复制到文件中: ```python from aliyunsdkcore.client import AcsClient from aliyunsdkcore.request import CommonRequest client = AcsClient('<your access key>', '<your access secret>', 'cn-shanghai') def set_gpio(status): request = CommonRequest() request.set_method('POST') request.set_domain('iot.cn-shanghai.aliyuncs.com') request.set_version('2018-01-20') request.set_action_name('SetDeviceProperty') request.add_query_param('RegionId', 'cn-shanghai') request.add_query_param('ProductKey', '<your product key>') request.add_query_param('DeviceName', '<your device name>') request.add_query_param('Payload', '{"LightSwitch":' + status + '}') response = client.do_action_with_exception(request) print(response) while True: status = input("请输入状态(0/1):") if status == '0': set_gpio('false') elif status == '1': set_gpio('true') else: print("输入有误,请重新输入") ``` 代码中需要进行替换的参数有: - <your access key>: 阿里云账号的Access Key - <your access secret>: 阿里云账号的Access Secret - <your product key>: 设备所属产品的Product Key - <your device name>: 设备的名称 5. 运行程序 在终端中运行Python程序: ``` python aliyun-iot.py ``` 程序会一直运行,等待输入状态。当输入状态为0时,GPIO 17口输出低电平,灯灭;当输入状态为1时,GPIO 17口输出高电平,灯亮。 到这里,手机通过阿里云物联网平台就可以远程控制树莓派GPIO口了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值