【问题标题】:asp.net file upload web service - could not find a part of the pathasp.net 文件上传网络服务 - 找不到路径的一部分
【发布时间】:2016-08-14 17:16:48
【问题描述】:

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

我的路径 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>

任何帮助将不胜感激。

【问题讨论】:

  • 你试过Server.MapPath()
  • 哪一行代码返回了那个错误?
  • 感谢您的回答!我试图找到返回错误的行,发现文件上传给了我“c://fakepath/file”而不是实际的文件路径。我在网上搜索但找不到修复它并获得实际路径的方法......

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


【解决方案1】:

文件上传的行为是正确的。 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# 面试中都会被问到。

【讨论】:

    猜你喜欢
    • 2023-03-29
    • 1970-01-01
    • 2016-08-27
    • 1970-01-01
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    相关资源
    最近更新 更多