当前位置:  编程技术>.net/c#/asp.net
本页文章导读:
    ▪Silverlight 实现文件上传      通过web进行文件上传,一般要使用form表单,在silverlight里面没有内置form表单功能,参考牛人文章,实现Silverlight文件上传。只要Web.config文件中httpRuntime节点的maxRequestLength属性足够大,上传就.........
    ▪Silverlight 实现文件下载      文件下载通常采用附件的方式在http响应头里面设置Content-disposition值为"attachment;filename=" + filepath,以及为Content-Length设置下载文件总长度(单位字节)来实现。本文是采用Silverlight实现文件下载,服.........
    ▪将不确定变为确定~transactionscope何时提升为分布式事务~续      之前写过一篇关于《将不确定变为确定~transactionscope何时提升为分布式事务》的文章,但今天又测试了一下,发现前一篇文章有很多问题,这里再把问题说一下。一 什么时间会把你的transactio.........

[1]Silverlight 实现文件上传
    来源:    发布时间: 2013-10-28

通过web进行文件上传,一般要使用form表单,在silverlight里面没有内置form表单功能,参考牛人文章,实现Silverlight文件上传。只要Web.config文件中httpRuntime节点的maxRequestLength属性足够大,上传就不受限制。

项目结构图:


MainPage类源码:

1 namespace SilverlightDownAndUploadFile
2 {
3 public partial class MainPage : UserControl
4 {
5 private OpenFileDialog ofd;
6 private AutoResetEvent autoEvent = new AutoResetEvent(false);
7
8 public MainPage()
9 {
10 InitializeComponent();
11 }
12
13 private static HttpWebRequest CreateWebRequest(string requestUriString, string httpMethod)
14 {
15 try
16 {
17 string ticks = Guid.NewGuid().ToString();
18 requestUriString = requestUriString + "/" + ticks;
19 HttpWebRequest request = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(new Uri(requestUriString));
20 request.Method = httpMethod;
21
22 return request;
23 }
24 catch (Exception ex)
25 {
26 return null;
27 }
28 }
29 #region 上传
30
31 private void ReadFileToRequestStream(BinaryReader fileStream, HttpWebRequest request)
32 {
33 try
34 {
35 request.BeginGetRequestStream((requestAsyncResult) =>
36 {
37 Stream requestStream = null;
38 try
39 {
40 requestStream = request.EndGetRequestStream(requestAsyncResult);
41 if (null != requestStream)
42 {
43 SilverlightMultipartFormData form = new SilverlightMultipartFormData();
44 //form.AddFormField("devilField", "中国人");
45 int maxBufferLen = 25 * 1024;
46 byte[] buffer = new byte[maxBufferLen];
47 int bytesRead = fileStream.Read(buffer, 0, maxBufferLen);
48 int i = 0;
49 while (bytesRead != 0)
50 {
51 i++;
52 byte[] newBuffer = GetNewBuffer(buffer, bytesRead);
53 form.AddStreamFile("package" + i, "package" + i, newBuffer);
54 buffer = new byte[maxBufferLen];
55 bytesRead = fileStream.Read(buffer, 0, maxBufferLen);
56 }
57
58 form.PrepareFormData();//结束
59 fileStream.Close();
60 request.ContentType = "multipart/form-data; boundary=" + form.Boundary;
61 foreach
    
[2]Silverlight 实现文件下载
    来源:    发布时间: 2013-10-28

文件下载通常采用附件的方式在http响应头里面设置Content-disposition值为"attachment;filename=" + filepath,以及为Content-Length设置下载文件总长度(单位字节)来实现。
本文是采用Silverlight实现文件下载,服务端用asp.net一般处理程序(.ashx)处理。
以下是项目结构图:

MainPage类源码如下:

1 namespace SilverlightDownAndUploadFile
2 {
3 public delegate void SaveFileHandler(int bytesRead, byte[] buffer);
4 public delegate void UploadFileHandler(int bytesRead);
5 public partial class MainPage : UserControl
6 {
7 private SaveFileDialog sfd;
8 private OpenFileDialog ofd;
9 Stream stream = null;
10 BinaryWriter bw = null;
11 private long fileLen = 0;
12 private long readFileLen = 0;//已下载长度
13 private AutoResetEvent autoEvent = new AutoResetEvent(false);
14
15 public MainPage()
16 {
17 InitializeComponent();
18 }
19
20 #region 下载
21 private void ShowDownResult(bool isSuccess)
22 {
23 this.Dispatcher.BeginInvoke(
24 () =>
25 {
26 if (isSuccess)
27 MessageBox.Show("下载完毕.");
28 else
29 MessageBox.Show("下载出错!");
30 });
31 }
32
33 private void UpdateDownProgress(int bytesRead)
34 {
35 readFileLen += bytesRead;
36
37 double rate = Convert.ToDouble(readFileLen) / Convert.ToDouble(fileLen);
38 double percent = Math.Round(rate, 2) * 100;
39 downProgress.Visibility = Visibility.Visible;
40 downProgress.pgBar.Value = percent;
41 downProgress.txtProgress.Text = percent + "%";
42
43 }
44
45 private void FlushToFile(int bytesRead, byte[] buffer)
46 {
47 try
48 {
49 if (null == stream)
50 {
51 stream = sfd.OpenFile();
52 bw = new BinaryWriter(stream);
53 }
54
55 bw.Write(buffer, 0, bytesRead);
56 bw.Flush();
57 UpdateDownProgress(bytesRead);
58 autoEvent.Set();
59 }
60 catch (Exception ex)
61 { }
62 }
63
64 private void RequestAndDownFile()
65 {
66 try
    
[3]将不确定变为确定~transactionscope何时提升为分布式事务~续
    来源:    发布时间: 2013-10-28

之前写过一篇关于《将不确定变为确定~transactionscope何时提升为分布式事务》的文章,但今天又测试了一下,发现前一篇文章有很多问题,这里再把问题说一下。

一 什么时间会把你的transactionscope提升为分布式事务,即要使用MSDTC服务

  •   在对同一个库进行操作时,它不会提升为分布式事务
  •   对于同一个库,建立多个数据上下文时,它不会提升为分布式事务
  •   当你操作两个库的表,这时才会提升为分布式事
  • 二 案例分析:

    public class DbBase : Commons.Data.DbContextRepositoryBase
    {

    public DbBase()
    : base(Commons.Data.DbFactory.Intance(System.Configuration.ConfigurationManager.ConnectionStrings["testEntities1"].ToString(), 2, 0))
    {

    }
    }

    public class DbBase2 : Commons.Data.DbContextRepositoryBase
    {

    public DbBase2()
    : base(Commons.Data.DbFactory.Intance(System.Configuration.ConfigurationManager.ConnectionStrings["testEntities2"].ToString(), 2, 1))
    {

    }
    }
    public class ReviewRepository : DbBase
    {

    }
    public class TestRepository : DbBase
    {
    public void Insert()
    {
    var product = new Product
    {
    Info = "test",
    Name = "test",
    };

    var product_Comment = new Product_Comment
    {
    CommentInfo = "test",
    CommentTitle = "Test",
    ProductID = 1,
    UserID = 1
    };
    var review = new Review
    {
    CreateDate = DateTime.Now,
    Info = "test",
    ObjID = 1,
    ObjType = 1,
    Status = 100,
    UserID = 1,
    };
    using (var trans = new TransactionScope())
    {
    //var testEntities = new testEntities();
    // var testEntities2 = new testEntities();
    #region 一个dbcontext对象不发生MSDTC
    //testEntities.Product.AddObject(product);
    //testEntities.Review.AddObject(review);
    //testEntities.SaveChanges();
    #endregion

    #region 多个dbcontext对象也不发生MSDTC
    //testEntities.Product.Add(product);
    //testEntities.SaveChanges();
    //testEntities2.Review.Add(review);
    //testEntities2.SaveChanges();
    #endregion

    #region 自己生产的DbContext对象也没有发生MSDTC
    // base.Insert<Product>(product);
    base.Insert<Product_Comment>(product_Comment);
    new ReviewRepository().Insert<Review>(review);
    #endregion
    trans.Complete();
    }
    }
    }

    测试环境:SQLSERVER2008在一台服务器

         IIS7在别一台服务器

    感谢阅读!

     

     

    本文链接


        
    最新技术文章:
     




    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3