2012-04-24 53 views
0

我從Android調用這個PHP代碼將文件從Android傳輸到服務器,並且它的工作非常好。ASP.NET文件上傳頁面中的「500錯誤」?

<?php 
$target_path = "./"; 
$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
echo "The file ". basename($_FILES['uploadedfile']['name']). 
" has been uploaded"; 
} else{ 
echo "There was an error uploading the file, please try again!"; 
} 
?> 

但我的服務器應用程序是在.NET和我必須寫這個代碼在.NET 我試着寫.NET版本代碼,但它不能正常工作,並返回500 internal server error

public partial class uploadfiles : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     try 
     { 
      HttpFileCollection uploadFile = Request.Files; 
      if (uploadFile.Count > 0) 
      { 
       HttpPostedFile postedFile = uploadFile[0]; 
       System.IO.Stream inStream = postedFile.InputStream; 
       byte[] fileData = new byte[postedFile.ContentLength]; 
       inStream.Read(fileData, 0, postedFile.ContentLength); 
       postedFile.SaveAs(Server.MapPath("Data") + "\\" + postedFile.FileName); 
      }   
     } 
     catch (Exception ex) 
     { 
      StringBuilder sb = new StringBuilder(); 
      sb.AppendLine("Message : " +ex.Message); 
      sb.AppendLine("Source : " + ex.Source); 
      sb.AppendLine("StackTrace : " + ex.StackTrace); 
      sb.AppendLine("InnerException : " + ex.InnerException); 
      sb.AppendLine("ToString : " + ex.ToString()); 

      LogInToFile(sb.ToString()); 
     } 
    } 
} 

它不記錄任何異常,甚至我認爲它沒有達到其第一行。我通過日誌文件檢查它。因爲它不起作用。請幫忙。

Android的代碼如下

HttpURLConnection connection = null; 
DataOutputStream outputStream = null; 
DataInputStream inputStream = null; 

String pathToOurFile = "/data/file_to_send.mp3"; 
String urlServer = "http://192.168.1.1/handle_upload.php"; 
String lineEnd = "\r\n"; 
String twoHyphens = "--"; 
String boundary = "*****"; 

int bytesRead, bytesAvailable, bufferSize; 
byte[] buffer; 
int maxBufferSize = 1*1024*1024; 

try 
{ 
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile)); 

URL url = new URL(urlServer); 
connection = (HttpURLConnection) url.openConnection(); 

// Allow Inputs & Outputs 
connection.setDoInput(true); 
connection.setDoOutput(true); 
connection.setUseCaches(false); 

// Enable POST method 
connection.setRequestMethod("POST"); 

connection.setRequestProperty("Connection", "Keep-Alive"); 
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 

outputStream = new DataOutputStream(connection.getOutputStream()); 
outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd); 
outputStream.writeBytes(lineEnd); 

bytesAvailable = fileInputStream.available(); 
bufferSize = Math.min(bytesAvailable, maxBufferSize); 
buffer = new byte[bufferSize]; 

// Read file 
bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

while (bytesRead > 0) 
{ 
outputStream.write(buffer, 0, bufferSize); 
bytesAvailable = fileInputStream.available(); 
bufferSize = Math.min(bytesAvailable, maxBufferSize); 
bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
} 

outputStream.writeBytes(lineEnd); 
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

// Responses from the server (code and message) 
serverResponseCode = connection.getResponseCode(); 
serverResponseMessage = connection.getResponseMessage(); 

fileInputStream.close(); 
outputStream.flush(); 
outputStream.close(); 
} 
catch (Exception ex) 
{ 
//Exception handling 
} 
+0

檢查事件日誌服務器上(WINR,' eventvwr.msc') - 它有任何相關的條目嗎? – penartur 2012-04-24 05:12:08

+0

你試圖上傳的文件大小是多少? – Ramesh 2012-04-24 07:03:03

+0

文件大小几乎是4-5 MB ...不多。 – Azhar 2012-04-24 07:06:38

回答

2

在ASP.NET應用程序的默認最大文件大小限制爲4 MB。您可以將應用程序配置通過

<system.web> 
    <httpRuntime maxRequestLength="20480" /> 
</system.web> 

設置如下web.config中接受更大尺寸要了解更多有關配置文件的大小限制退房Large file uploads in ASP.NET

1

試試這個:

postedFile.SaveAs(Server.MapPath("~/Data/" + postedFile.FileName)); 

的地方〜/數據/是您要保存文件的位置。

然後+ postedFile.FileName您的文件名。

問候

相關問題