Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > Windows Phone Dev - สอนเขียน App บนโปรแกรม Windows Phone 7 , Windows Phone 8 > Windows Phone Download Save file to Isolated Storage (Application Storage)



Clound SSD Virtual Server

Windows Phone Download Save file to Isolated Storage (Application Storage)

Windows Phone Download Save file to Isolated Storage (Application Storage) ตัวอย่างการเขียนโปรแกรมบน Windows Phone ในการ ดาวน์โหลด (Download) ไฟล์ที่อยู่บน URL (Website) บน Internet และทำการ Save ไฟล์ทีได้จากการดาวน์โหลดลงใน Isolated Storage บน Application ของ Windows Phone

Windows Phone Download Save file to Isolated Storage

Download Save file to Isolated Storage


Basic Windows Phone and Isolated Storage (Application Storage)

สำหรับพื้นฐาน Isolated Storage กับ Windows Phone ควรอ่าน 2 บทความนี้ เพื่อความเข้าใจ


Example ตัวอย่างการ Download และ Save ไฟล์จากเว็บไซต์ลงใน Isolated Storage บน Application ของ Windows Phone

MainPage.xaml

Windows Phone Download Save file to Isolated Storage

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="9,291,0,0" Name="txtResult" Text="Result" VerticalAlignment="Top" Width="441" TextAlignment="Center" />
            <Button Content="Start Download" Height="72" HorizontalAlignment="Left" Margin="106,81,0,0" Name="btnDownload" Click="btnDownload_Click" VerticalAlignment="Top" Width="238" />
            <ProgressBar Height="4" HorizontalAlignment="Left" Margin="0,189,0,0" Name="ProgressBar1" VerticalAlignment="Top" Width="460" />
        </Grid>
        
    </Grid>









MainPage.xaml.vb (VB.NET)
Imports System.IO
Imports System.Text
Imports System.IO.IsolatedStorage

Partial Public Class MainPage
    Inherits PhoneApplicationPage

    ' Constructor
    Public Sub New()
        InitializeComponent()
    End Sub

    Dim client As WebClient
    Dim prog As ProgressIndicator
    Dim url As String

    Private Sub btnDownload_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)

        Me.txtResult.Text = "Downloading..."

        client = New WebClient()
        url = "http://localhost/myphp/music.mp3"
        Dim uri As New Uri(url)

        AddHandler client.OpenReadCompleted, AddressOf client_OpenReadCompleted
        AddHandler client.DownloadProgressChanged, AddressOf client_DownloadProgressChanged
        client.OpenReadAsync(uri)

    End Sub

    Private Sub client_OpenReadCompleted(sender As Object, e As OpenReadCompletedEventArgs)
        If e.Cancelled = False AndAlso e.Error Is Nothing Then

            Dim strFileName As String = url.Substring(url.LastIndexOf("/") + 1, (url.Length - url.LastIndexOf("/") - 1))
            Dim isoStore As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication() ' Path Storage

            '*** If File Exists 
            If isoStore.FileExists(strFileName) Then
                isoStore.DeleteFile(strFileName)
            End If

            Dim dataFile As New IsolatedStorageFileStream(strFileName, FileMode.CreateNew, isoStore)


            Dim fileLen As Long = e.Result.Length
            Dim b As Byte() = New Byte(fileLen - 1) {}
            e.Result.Read(b, 0, b.Length)
            dataFile.Write(b, 0, b.Length)
            dataFile.Flush()

            Dim lenghtOfFile = dataFile.Length

            Me.txtResult.Text = "Download Completed."

        End If
    End Sub

    Private Sub client_DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs)
        Me.ProgressBar1.Value = e.ProgressPercentage
    End Sub

End Class

MainPage.xaml.cs (C#)
using System;
using System.Windows;
using System.Net;
using System.IO;
using System.Text;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace PhoneApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        WebClient client;
        ProgressIndicator prog;
        string url;

        private void btnDownload_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.txtResult.Text = "Downloading...";
            client = new WebClient();
            url = "http://localhost/myphp/music.mp3";
            Uri uri = new Uri(url);
            client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
            client.OpenReadAsync(uri);
        }

        private void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Cancelled == false && e.Error == null)
            {
                string strFileName = url.Substring(url.LastIndexOf("/") + 1, (url.Length - url.LastIndexOf("/") - 1));

                IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
                //  Path Storage
                // *** If File Exists 
                if (isoStore.FileExists(strFileName))
                {
                    isoStore.DeleteFile(strFileName);
                }
                IsolatedStorageFileStream dataFile = new IsolatedStorageFileStream(strFileName, FileMode.CreateNew, isoStore);
                long fileLen = e.Result.Length;
                byte[] b = new byte[fileLen];
                e.Result.Read(b, 0, b.Length);
                dataFile.Write(b, 0, b.Length);
                dataFile.Flush();
                object lenghtOfFile = dataFile.Length;
                this.txtResult.Text = "Download Completed.";
            }
        }

        private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            this.ProgressBar1.Value = e.ProgressPercentage;
        }
    }

}

ในตัวอย่างนี้มี Code ทั้งที่เป็น VB.NET และ C# และสามารถดาวน์โหลด All Code ทั้งหมดได้จากส่วนท้ายของบทความ (Login สมาชิกก่อน)








Screenshot

Windows Phone Download Save file to Isolated Storage

คลิก Start Download เพื่อเริ่มการดาวน์โหลดไฟล์

Windows Phone Download Save file to Isolated Storage

ไฟล์กำลังดาวน์โหลด โดยจะมีการแสดง ProgressBar เพื่อแสดงสถานะของการดาวน์โหลดด้วย

Windows Phone Download Save file to Isolated Storage

ไฟล์ดาวน์โหลดเรียบร้อย

Windows Phone Download Save file to Isolated Storage

ตรวจสอบไฟล์บน Isolated Storage ของ Windows Phone ที่ได้จากการดาวน์โหลด

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-09-15 11:03:32 / 2017-03-25 22:15:37
  Download : Download  Windows Phone Download Save file to Isolated Storage (Application Storage)
 Sponsored Links / Related

 
Windows Phone and Isolated Storage (Application Storage)
Rating :

 
Windows Phone ListBox Image Binding and Resource from Isolated Storage (Application Storage)
Rating :

 
Windows Phone Play MP3 or Video MediaElement from Isolated Storage (Application Storage)
Rating :

 
Windows Phone Copy Transfer file from PC Between Isolated Storage (Application Storage)
Rating :

 
Windows Phone Create and Write Text File In Isolated Storage (Application Storage)
Rating :

 
Windows Phone Read Text File in Isolated Storage (Application Storage)
Rating :

 
Windows Phone Delete / Remove File In Isolated Storage (Application Storage)
Rating :

 
Windows Phone List Show File In Isolated Storage (Application Storage)
Rating :

 
Windows Phone Image Source From Isolated Storage (Application Storage)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 00
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่