首页 新闻 会员 周边

关于sl+wcf上传文件的问题

1
悬赏园豆:10 [已解决问题] 解决于 2011-10-26 14:33

我使用sl+wcf上传文件,用vs能正常运行,但是部署到iis

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;

namespace UploadTest.Web
{
[ServiceContract(Namespace
= "")]

[AspNetCompatibilityRequirements(RequirementsMode
= AspNetCompatibilityRequirementsMode.Allowed)]

public class UploadService
{

[OperationContract]

public void ActionUpload(string fileName, byte[] fileContext, bool isAppend)
{

string uploadFolder = System.Web.Hosting.HostingEnvironment.MapPath("~/upload");



if (!System.IO.Directory.Exists(uploadFolder))
{

System.IO.Directory.CreateDirectory(uploadFolder);

}



System.IO.FileMode fileMode
= System.IO.FileMode.Create;



if (isAppend)
{

fileMode
= System.IO.FileMode.Append;

}



using (System.IO.FileStream fs = new System.IO.FileStream(uploadFolder + @"\" + fileName, fileMode, System.IO.FileAccess.Write))
{

fs.Write(fileContext,
0, fileContext.Length);

}

}

}


}
以上是wcf文件的代码


<UserControl x:Class="UploadTest.MainPage"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable
="d"
d:DesignHeight
="300" d:DesignWidth="400">
<Canvas>
<Button Content="点击上传" Height="47" Margin="60,66,62,57" Name="button1" Width="211" Click="button1_Click" />
</Canvas>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.ComponentModel;
using System.Windows.Browser;
using UploadTest.ServiceReference1;

namespace UploadTest
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
try
{
// HtmlPage.Window.Alert(button1.Content.ToString());
OpenFileDialog openFileDialog = new OpenFileDialog();

string filterType = "图片(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|所有文件(*.*)|*.*";

openFileDialog.Filter
= filterType;

//是否选择多个文件

openFileDialog.Multiselect
= false;



if (openFileDialog.ShowDialog() == true)
{

FileInfo fileInfo
= openFileDialog.File;

UploadFileInfo uploadFile
= new UploadFileInfo();

uploadFile.FileName
= Guid.NewGuid().ToString() + fileInfo.Extension;

//使用字节流上传

Stream stream
= fileInfo.OpenRead();

uploadFile.FileSize
= stream.Length / 1024;

uploadFile.FileByte
= new List<byte[]>();



int b;

while (stream.Position > -1 && stream.Position < stream.Length)
{

if (stream.Length - stream.Position >= 3000)
{

b
= 3000;

}

else
{

b
= (int)(stream.Length - stream.Position);

}



byte[] filebyte = new byte[b];

stream.Read(filebyte,
0, b);

uploadFile.FileByte.Add(filebyte);

}

stream.Close();



UploadServiceClient client
= new UploadServiceClient();

client.ActionUploadCompleted
+= new EventHandler<AsyncCompletedEventArgs>(client_ActionUploadCompleted);

client.ActionUploadAsync(uploadFile.FileName, uploadFile.FileByte[
0], false, uploadFile);

}
}
catch (Exception ex)
{
HtmlPage.Window.Alert(ex.Message);
}

}



void client_ActionUploadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
try
{

if (e.Error == null)
{

UploadFileInfo uploadFile
= (UploadFileInfo)e.UserState;

uploadFile.FileCompletedByte
+= uploadFile.FileByte[0].Length / 1024;

uploadFile.FileByte.RemoveAt(
0);



if (uploadFile.FileByte.Count == 0)
{

HtmlElement element
= HtmlPage.Document.GetElementById("GetUploadFileName");

// element.SetAttribute("value", uploadFile.FileName);



button1.Content
= "恭喜您,上传成功";

}

else
{

(sender
as UploadServiceClient).ActionUploadAsync(uploadFile.FileName, uploadFile.FileByte[0], true, uploadFile);

double progressBar = uploadFile.FileCompletedByte / uploadFile.FileSize;

button1.Content
= progressBar.ToString("0%");

}

}
}
catch (Exception ex)
{
HtmlPage.Window.Alert(ex.Message);
}
}
}
}

以上为xamp中的代码

后就不正常了,求高手讲解,代码贴出来了,谢谢各位高手了。

Ben Chan的主页 Ben Chan | 初学一级 | 园豆:3
提问于:2011-07-27 16:18
< >
分享
最佳答案
0

IIS中你需要在某个目录写文件(System.IO.FileAccess.Write),把这个目录的权限 赋予IIS的运行账号Network Services 就行了 

VS中运行时使用的是你当前登录的账号权限因此是可以的

收获园豆:10
2012 | 高人七级 |园豆:21228 | 2011-07-28 08:11
这位兄台,我按照你的办法做了,还是不行,不知道还有其它什么原因呢?
Ben Chan | 园豆:3 (初学一级) | 2011-07-28 15:07
不会吧,你用事件查看器看看记录了什么内容,这里应该有线索
2012 | 园豆:21228 (高人七级) | 2011-07-29 16:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册