asp.net文件上传web服务 - 找不到路径的一部分

时间:2016-04-21 13:12:43

标签: c# asp.net web-services file-upload

我目前正在尝试使用visual studio 2012创建文件上传网络服务。但我的代码返回“找不到路径的一部分”+我的路径。

我的路径存在100%。

这是代码:

网络服务:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Data;
using System.Web.Services.Protocols;
using System.ComponentModel;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class OGcloud : System.Web.Services.WebService 
{

public OGcloud () {}

[WebMethod]
public string UploadFile(byte[] f, string fileName)
{
    try
    {
        MemoryStream ms = new MemoryStream(f);
        FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath
                    ("~/TransientStorage/") + fileName, FileMode.Create);
        ms.WriteTo(fs);
        ms.Close();
        fs.Close();
        fs.Dispose();

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

aspx.cs文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Data;

public partial class User_MyParking : System.Web.UI.Page
{
public string msg = "";
protected void Page_Load(object sender, EventArgs e)
{
    if (Request.Form["loct"] != null)
    {
        byte[] f = OGcloud_Upload.FileBytes;
        OGcloud.OGcloudSoapClient access = new OGcloud.OGcloudSoapClient();
        msg = access.UploadFile(f, OGcloud_Upload.FileName);
    }
}
public override void VerifyRenderingInServerForm(Control control)
{
    //base.VerifyRenderingInServerForm(control);
}
}

aspx表单:

    <table class="Casual Center" dir="rtl" style="border-spacing:10px">
        <tr>
            <td colspan="2"><input type="text" name="loct" id="loct"   style="width:300px"/></td>
        </tr>
        <tr>
            <td colspan="2"><textarea id="info" name="info" draggable="false" style="width:950px; height:150px; resize:none;" class="Casual"></textarea></td>
        </tr>
        <tr>
            <td colspan="2" id="Err_info"></td>
        </tr>
        <tr>
            <td colspan="2" runat="server">
                <asp:FileUpload ID="OGcloud_Upload" runat="server" />
            </td>
        </tr>
    </table>

任何帮助都将非常感激。

1 个答案:

答案 0 :(得分:0)

文件上传的行为是正确的。 Web服务在客户端计算机上查看实际路径存在安全风险。您可以访问该文件的内容,因此不需要特定的路径。

这是你的代码:

FileStream fs = new FileStream(
    System.Web.Hosting.HostingEnvironment.MapPath("~/TransientStorage/") + 
                   fileName, 
                FileMode.Create);

这是正确的代码(假设您在文件顶部“使用System.IO”)如下:

// Make sure TransientStorage exists in your virtual folder root
string path = System.Web.Hosting.HostingEnvironment.MapPath(
                     @"~/TransientStorage");
string filenameOnly = Path.GetFileName(fileName);
string fullyQualifiedFill = Path.Combine(path, filenameOnly);

FileStream fs = new FileStream(fullyQualifiedFill, FileMode.Create);

请同时使用IDisposable:

using (MemoryStream ms = new MemoryStream(f))
{
    using (FileStream fs = <see code above> )
    {
        ms.WriteTo(fs);
        fs.Flush(); // may not be needed but better to be safe
        fs.Close();
    }

    ms.Close();
}

仅供参考:知道IDisposable ......在每次C#面试时都会被问到。

相关问题