为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

HUWWW 2018-12-07 00:04:35
我使用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中的代码 后就不正常了,求高手讲解,代码贴出来了,谢谢各位高手了。
查看完整描述

1 回答

?
芜湖不芜

TA贡献1796条经验 获得超7个赞

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

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

查看完整回答
反对 回复 2019-01-21
  • 1 回答
  • 0 关注
  • 414 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信