使用Web服务上传文件 [英] File Upload using web service

查看:120
本文介绍了使用Web服务上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Web服务从silverlight客户端上传文件。我设法使用下面的代码[webservice]从同一台计算机成功上传文件,但我从网络上传时出错。



我尝试修复如将文件权限设置为完整,但没有一个工作。



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Services;
使用 System.IO;
使用 System.Web.Hosting;
使用 System.Security;

命名空间 SilverlightApplication5.Web
{
[WebService(Namespace = http://***.com/testfile/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem( false )]
public class FileUploadService:System.Web.Services.WebService
{
[WebMethod]
public string TestService()
{
return 服务测试成功;
}

[WebMethod]
public string 上传(字符串 id,字符串模式,字符串 path, string name, string filedata, bool overwrite, string 标签, bool final)
{
尝试
{
string filename = string .Empty;

if (Directory.Exists(@ HostingEnvironment.ApplicationPhysicalPath + / Documents / + path)== false
{
Directory.CreateDirectory( @ HostingEnvironment.ApplicationPhysicalPath + / Documents / + path);
}
尝试
{
filename = Server.MapPath( )+ @ \ Documents \ + path.Replace( / @ \)+ @ \ + name;

if (mode == new
{
if (File.Exists(filename)== true
{
if (overwrite)
{
File.Delete(filename) ;
}
其他
{
返回 文件已存在;
}
}

WriteFile(filename,Convert.FromBase64String(filedata),FileMode.Create);
}
else
{
WriteFile(filename,Convert.FromBase64String(filedata),FileMode.Append);
}
}
catch (例外情况)
{
File.Delete(filename);
return 文件写入错误: + ex.Message;
}

return OK;
}
catch (SecurityException ex)
{
return ex.Message.ToString();
}
catch (例外情况)
{
return ex.Message.ToString();
}
}

私有 void WriteFile( string filename, byte [] content,FileMode fileMode)
{
Stream target = null ;
BinaryWriter writer = null ;

try
{
target = File.Open(filename,fileMode);
writer = new BinaryWriter(target);

writer.Write(content);
}
catch (例外情况)
{
throw new 异常( 无法写入文件: + filename + - + ex.Message);
}
最后
{
如果(目标!= null
{
target.Close();
}
if (writer!= null
{
writer.Close();
}
}
}
}
}





请告诉我在托管这项服务时做错了。

提前谢谢。

解决方案

在检查代码之前,请确保在客户端浏览器设置中启用了保存文件。如果不是,您将无法写入磁盘并保存文件。



其次,在Silverlight应用程序中,您应该只写入应用程序数据存储:



  if (Application.Current.HasElevatedPermissions)
{
string perUserApplicationData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
}





您可以查看此信息以更熟悉受信任的应用程序权限:

https://msdn.microsoft.com/en-us/library/ee721082(v=vs.95).aspx [ ^ ]


I am using Web service to upload file from silverlight client. I managed to upload files successfully using below code[webservice] from same computer but i get error on uploading from network.

I tried fixes like setting file permissions to full, but none worked.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Web.Hosting;
using System.Security;

namespace SilverlightApplication5.Web
{
    [WebService(Namespace = "http://***.com/testfile/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class FileUploadService : System.Web.Services.WebService
    {
        [WebMethod]
        public string TestService()
        {
            return "Service test suceed";
        }

        [WebMethod]
        public string Upload(string id, string mode, string path, string name, string filedata, bool overwrite, string tag, bool final)
        {
            try
            {
                string filename = string.Empty;

                if (Directory.Exists(@HostingEnvironment.ApplicationPhysicalPath + "/Documents/" + path) == false)
                {
                    Directory.CreateDirectory(@HostingEnvironment.ApplicationPhysicalPath + "/Documents/" + path);
                }
                try
                {
                    filename = Server.MapPath("~") + @"\Documents\" + path.Replace("/", @"\") + @"\" + name;

                    if (mode == "new")
                    {
                        if (File.Exists(filename) == true)
                        {
                            if (overwrite)
                            {
                                File.Delete(filename);
                            }
                            else
                            {
                                return "File Already Exists";
                            }
                        }

                        WriteFile(filename, Convert.FromBase64String(filedata), FileMode.Create);
                    }
                    else
                    {
                        WriteFile(filename, Convert.FromBase64String(filedata), FileMode.Append);
                    }
                }
                catch (Exception ex)
                {
                    File.Delete(filename);
                    return "File Write Error: " + ex.Message;
                }

                return "ok";
            }
            catch (SecurityException ex)
            {
                return ex.Message.ToString();
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        private void WriteFile(string filename, byte[] content, FileMode fileMode)
        {
            Stream target = null;
            BinaryWriter writer = null;

            try
            {
                target = File.Open(filename, fileMode);
                writer = new BinaryWriter(target);

                writer.Write(content);
            }
            catch (Exception ex)
            {
                throw new Exception("Could not write to file: " + filename + " - " + ex.Message);
            }
            finally
            {
                if (target != null)
                {
                    target.Close();
                }
                if (writer != null)
                {
                    writer.Close();
                }
            }
        }
    }
}



please tell me where i am doing wrong in hosting this service.
thanks in advance.

解决方案

Before checking the code, make sure that saving files is ENABLED in client browser settings. If it's not, you won't be able to write to the disk and save files.

Secondly, in the Silverlight application you should write to the Application Data storage only:

if (Application.Current.HasElevatedPermissions)
{
    string perUserApplicationData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
}



You can check this out to be more familiar with trusted applications permissions:
https://msdn.microsoft.com/en-us/library/ee721082(v=vs.95).aspx[^]


这篇关于使用Web服务上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆