台灣最大程式設計社群網站
線上人數
991
 
會員總數:239604
討論主題:185880
歡迎您免費加入會員
討論區列表 >> C# >> c#接收android上傳檔案問題
[ 變換順序 ]  
[我要回覆]
1
回應主題 加入我的關注話題 檢舉此篇討論 將提問者加入個人黑名單
c#接收android上傳檔案問題
價值 : 45 QP  點閱數:202 回應數:2

樓主

佚名
門外漢
0 5
218 5
發送站內信


php接收檔案程式碼:
https://drive.google.com/open?id=0B2tk1k2HlqOXQ3BTbUNDYkNJcVE

android送圖片程式碼:
https://drive.google.com/open?id=0B2tk1k2HlqOXLTlidnU1amp5cEk


這個程式是android上傳檔案到php的程式碼,測試是可以運作的,也增加了6.0的權限判斷

android它送程式的方式是透過以下程式上傳:

HttpResponse response = httpclient.execute(httppost);

php它逶過$_FILES收資料再搬移,因為asp.net(c#)似乎沒有$_FILES之類的參數,

如果以上的寫法改為c#收資料各位會怎麼想?






這個是andorid的程式段:
private String uploadFile() {
String responseString = null;
//
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost;
if(select == 1){
httppost = new HttpPost(FILE_UPLOAD_URL);
}else if(select == 2){
httppost = new HttpPost(FILE_UPLOAD_URL2);
}else{
httppost = new HttpPost(FILE_UPLOAD_URL);
}
//
//
try {
AndroidMultiPartEntity entity = new AndroidMultiPartEntity(
new AndroidMultiPartEntity.ProgressListener() {
public void transferred(long num) {
publishProgress((int) ((num / (float) totalSize) * 100));
}
});

File sourceFile = new File(filePath);
entity.addPart("image", new FileBody(sourceFile));



// Extra parameters if you want to pass to server
// entity.addPart("website",
// new StringBody("www.androidhive.info"));
// entity.addPart("email", new StringBody("abc@gmail.com"));

totalSize = entity.getContentLength();
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();

int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
// Server response
responseString = EntityUtils.toString(r_entity);
} else {
responseString = "Error occurred! Http Status Code: "
+ statusCode;
}

} catch (ClientProtocolException e) {
responseString = e.toString();
} catch (IOException e) {
responseString = e.toString();
}
return responseString;

}
//
@Override
protected void onPostExecute(String result) {
Log.e("END", "Response from server: " + result);
showAlert(result);
super.onPostExecute(result);
}
//
}




這個是pho收檔案的程式
<?php
echo $_FILES['image']['name'] . '<br/>';

$target_path = "uploads/";
$target_path = $target_path . basename($_FILES['image']['name']);

try {
if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
throw new Exception('Could not move file');
}

echo "The file " . basename($_FILES['image']['name']) .
" has been uploaded";
} catch (Exception $e) {
die('File did not upload: ' . $e->getMessage());
}
?>

搜尋相關Tags的文章: [ asp.net ] ,
本篇文章發表於2017-04-18 18:34
別忘捐VP感謝幫助你的人 新手會員瞧一瞧
1樓
回應

四百龍銀
檢舉此回應
webapi upload file
本篇文章回覆於2017-04-19 11:37
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
2樓
作者回應

佚名
檢舉此回應
不好意思在測試幾種方案所以晚回覆
我看到至少有兩種做法

1、MVC版本上傳方式
>>這個我還在找空間測試


2、.ashx檔的版本:
檔案的架構大致如下(js的部份省略):
Default.aspx
TriggerUpload.aspx
Upload.ashx
web.config

<a href="http://s17.photobucket.com/user/ybddrawing/media/2017/_XCV1.png.html" target="_blank"><img src="http://i17.photobucket.com/albums/b91/ybddrawing/2017/_XCV1.png" border="0" alt=" photo _XCV1.png"/></a>

一般上傳透測試是可以的,但是如果和剛才的android程式則不行
以下是它收資料的程式碼(Upload.ashx)

一般網頁上傳的介面,最底下Upload : IHttpHandler為.ashx的程式碼,連結有完整的版本:
http://mapinfo.somee.com/FilesUpload/

android程式基本上相同,上傳網址改為:
http://mapinfo.somee.com/FilesUpload/Upload.ashx


回傳結果:
Response from Servers
Error: Object reference not set to and instance of an object.

我認為可能是參數的問題,這邊先貼出來詢問



public class Upload : IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Expires = -1;
try
{
HttpPostedFile postedFile = context.Request.Files["Filedata"];

string savepath = "";
string tempPath = "";
tempPath = System.Configuration.ConfigurationManager.AppSettings["FolderPath"];
savepath = context.Server.MapPath(tempPath);
string filename = postedFile.FileName;
if (!Directory.Exists(savepath))
Directory.CreateDirectory(savepath);

postedFile.SaveAs(savepath + @"\" + filename);
//context.Response.Write(tempPath + "/" + filename);//20170422測試
context.Response.Write(tempPath + "/uploads/" + filename);
context.Response.StatusCode = 200;
}
catch (Exception ex)
{
context.Response.Write("Error: " + ex.Message);
}
}

public bool IsReusable {
get {
return false;
}
}
}




本篇文章回覆於2017-04-22 20:19
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
[ 變換順序 ]   
1

回覆
如要回應,請先登入.