2016-04-21 64 views
0

我目前正在嘗試使用visual studio 2012創建一個文件上傳web服務。但我的代碼返回「找不到路徑的一部分」 +我的路徑。asp.net文件上傳web服務 - 找不到路徑的一部分

我的路徑存在100%。

下面的代碼:

Web服務:

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> 

任何幫助將是非常讚賞。

+2

你試過了嗎?Server.MapPath()' –

+0

哪行代碼返回那個錯誤? –

+0

謝謝你的回答!我試圖找到返回錯誤的行,發現fileupload給我「c:// fakepath/file」而不是實際的文件路徑。我在網上搜索,但無法找到一種方法來解決它,並得到實際的路徑... – omerg7

回答

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(); 
} 

FYI:知道了IDisposable ...據問每一個C#的採訪。

相關問題