【问题标题】:Image Upload in Silverlight ApplicationSilverlight 应用程序中的图像上传
【发布时间】:2015-11-02 05:55:34
【问题描述】:

当我尝试使用 Silverlight 应用程序将图像从本地驱动器上传到 ms sql 时,它会抛出异常“不允许文件操作。访问路径 'userimage.png' 被拒绝 ”。如何解决这个问题?我不擅长 Silverlight。请帮帮我。

我的代码是,

OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
string filename = ofd.File.Name;
FileInfo fInfo = new FileInfo(filename);
long numBytes = fInfo.Length;
FileStream fStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fStream);
byte[] data = br.ReadBytes((int)numBytes);

我在这里收到错误,“FileInfo fInfo = new FileInfo(filename);

堆栈跟踪是,

在 System.IO.FileSecurityState.EnsureState() 在 System.IO.FileInfo.Init(字符串文件名,布尔检查主机) 在 System.IO.FileInfo..ctor(字符串文件名) at AttendanceManagementSystem.InsertPopup.btnimg_Click(对象发送者,RoutedEventArgs e) 在 System.Windows.Controls.Primitives.ButtonBase.OnClick() 在 System.Windows.Controls.Button.OnClick() 在 System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) 在 System.Windows.Controls.Control.OnMouseLeftButtonUp(控制 ctrl,EventArgs e) 在 MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj,IntPtr unmanagedObjArgs,Int32 argsTypeIndex,Int32 actualArgsTypeIndex,字符串 eventName,UInt32 标志)

【问题讨论】:

标签: c# file-upload filestream silverlight-5.0 fileinfo


【解决方案1】:

最后,它现在工作正常。工作代码如下。

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
            bool? userClickedOK = ofd.ShowDialog();
            if (userClickedOK == true)
            {
                System.IO.Stream fileStream = ofd.File.OpenRead();
                FileStream fStream = ofd.File.OpenRead();
                BinaryReader br = new BinaryReader(fStream);
                byte[] imagedata = br.ReadBytes((int)fStream.Length);
            }

现在字节数组“imagedata”可以插入到db了。

【讨论】:

    猜你喜欢
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-02
    相关资源
    最近更新 更多