【发布时间】: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