Silverlight 上傳程式

來源:互聯網
上載者:User

 

  Handler.ashx

 

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;

namespace SilverlightUpFile.Web
{
    /// <summary>
    /// $codebehindclassname$ 的摘要說明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Handler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {    //擷取上傳的資料流
            Stream sr = context.Request.InputStream;
            try
            {
                //產生隨機的檔案名稱(本DEMO中的上傳圖片名稱也可用參數方法傳遞過來)
                string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                Random rnd = new Random();
                string filename = "";
                for (int i = 1; i <= 32; i++)
                {
                    filename += chars.Substring(rnd.Next(chars.Length), 1);
                }

                byte[] buffer = new byte[4096];
                int bytesRead = 0;
                //將當前資料流寫入伺服器端檔案夾ClientBin下
                using (FileStream fs = File.Create(context.Server.MapPath("ClientBin/" + filename + ".jpg"), 4096))
                {
                    while ((bytesRead = sr.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        //向檔案中寫資訊
                        fs.Write(buffer, 0, bytesRead);
                    }
                }

                context.Response.ContentType = "text/plain";
                context.Response.Write("上傳成功");
            }
            catch (Exception e)
            {
                context.Response.ContentType = "text/plain";
                context.Response.Write("上傳失敗, 錯誤資訊:" + e.Message);
            }
            finally
            {
                sr.Dispose();
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

Code
        private void OnUpLoadClick(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog()
            {
                Filter = "Jpeg Files (*.jpg)|*.jpg|All Files(*.*)|*.*",
                Multiselect = true
            };
            if ((bool)(openFileDialog.ShowDialog()))
            {
                fi = openFileDialog.File;
                WebClient webclient = new WebClient();

                webclient.OpenWriteCompleted += new OpenWriteCompletedEventHandler(webclient_OpenWriteCompleted);
                webclient.OpenWriteAsync(new Uri("http://localhost:3239/Handler.ashx", UriKind.Absolute), "POST");

            }
        }

 

 

 

Code
<UserControl x:Class="SilverlightUpFile.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Image  x:Name="myImage" Grid.Column="1" />
        <StackPanel Grid.Row="1" Background="white" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Stretch">
            <Button Grid.Row="1"
          Grid.Column="0"
          Content="選擇圖片"
          Margin="8" Click="OnClick" FontSize="16"  Width="100" Height="30"/>
            <Button Grid.Row="1"
          Grid.Column="2"
          Content="上傳該圖片"
          Margin="8" Click="OnUpLoadClick" FontSize="16" Width="100" Height="30" />
        </StackPanel>
    </Grid>
</UserControl>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.