When i upload a file on a Server, it shows an error


When i upload a file on a Server, it shows an error



When i am uploading file on server through Multipart library.Multipart is showing me file upload successfully but when i saw file in server there is no file on server.



I am using below code.Please check it where i am missing some code or anything else


String API_URL_POST_COMPLAINT
="";

String responseResult = "0";
try {
SharedPreferences sharedPreferences =
getApplicationContext().getSharedPreferences("lusername", Context.MODE_PRIVATE);
String userId = sharedPreferences.getString("username", "");

Uri uri = Uri.parse(result.getUri().toString());
String realPath = ImageFilePath.getPath(MainActivity.this, uri);
File picturePathFile = new File(realPath);

String complaint="Hello";
String apiUrl = API_URL_POST_COMPLAINT + "Token=" + "isuotb4hmtXrOcB0IhHdkDbL7NReoacMpYDHWRUFtWYj7l-Ps2Y=" + "&WOID="+URLEncoder.encode("52326", "UTF-8") +"&DocTitle="+URLEncoder.encode("Hi", "UTF-8")+"&FileName="+picturePathFile;
apiUrl.replaceAll(" ", "%20");
apiUrl.replaceAll("n", "%20");
MyMultipartRequest multipart = new MyMultipartRequest(getApplicationContext(), apiUrl);
multipart.addHeaderField("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
multipart.addHeaderField("Test-Header", "This is my test Header field");
multipart.addFormField("Token", "isuotb4hmtXrOcB0IhHdkDbL7NReoacMpYDHWRUFtWYj7l-Ps2Y=");
multipart.addFormField("WOID", "52326");
multipart.addFormField("DocTitle", "Hello");
multipart.addFilePart("FileName", picturePathFile);
System.out.println("complaintFile = " + complaint);
System.out.println("picturePathFile = " + picturePathFile);
System.out.println("complaintFile = " + picturePathFile.getName());
String responsePost = multipart.finish();
System.out.println("responsePost = " + responsePost);
return responsePost;
} catch (Exception e) {
e.printStackTrace();
responseResult = "0";
}
return responseResult;



.Net code is here


[HttpPost]
public Result UploadDoc(string Token, int WOID, string DocTitle, string
FileName)
{
if (!Request.Content.IsMimeMultipartContent() ||
HttpContext.Current.Request.Files.Count == 0)
return new Result(Result.ResultCode.NoAction, "File not found");

byte lBytes = new byte[HttpContext.Current.Request.Files(0).ContentLength -
1 + 1];
HttpContext.Current.Request.Files(0).InputStream.Read(lBytes, 0,
HttpContext.Current.Request.Files(0).ContentLength - 1);

Result lRes = UserClass.GetUserFromToken(Token);
if (lRes.NoSuccess)
return lRes;
UserClass lUser = (UserClass)lRes.RetInfo;
if (lUser.RoleType == UserClass.UserRoleType.Read_Only)
return lRes.Init(Result.ResultCode.NoAction, "Permission Denied for User
Role");
if (WorkOrderClass.SearchByUser(WOID.ToString(), lUser, true).Length == 0)
lRes.Init(Result.ResultCode.NoAction, "Permission Denied on WO");
return (new DocumentClass() { WorkOrderID = WOID, UploadedBy = lUser.UserID,
DocTitle = DocTitle.Trim(), FileName = FileName, Doc = lBytes
}).InsertIntoDB;
}





Please STOP! defacing your post title!
– Kling Klang
Jun 29 at 10:56




1 Answer
1



You have to add the file type (what kind of file you are uploading). And try to upload with okhttp.


private ProgressDialog pDialog;

private class FileAsync extends AsyncTask<String, String, String> {
@SuppressLint("ResourceType")
@Override
protected String doInBackground(String... params) {
String result = "";
try {
OkHttpClient okHttpClient = getOkHttpClient();
MultipartBody.Builder data = new MultipartBody.Builder();
data.setType(MultipartBody.FORM);
MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");

data.addFormDataPart("FileName","file.png",
RequestBody.create(MEDIA_TYPE_PNG, picturePathFile));

RequestBody requestBody = data.build();
Request request = new Request.Builder()
.url("Your URL").post(requestBody)
.build();
Response response = okHttpClient.newCall(request).execute();
result = response.body().string().toString();

Log.e("Result : ", result);
} catch (IOException ex) {
System.err.println(ex);
}
return result;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(activity);
pDialog.setMessage("Please wait...");
pDialog.show();
pDialog.setCanceledOnTouchOutside(false);
}

@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
if (pDialog != null && pDialog.isShowing()) {
pDialog.dismiss();
}

if (response != null) {
//Do something with your response.
}
}
}

public OkHttpClient getOkHttpClient() {
return new OkHttpClient.Builder()
.connectTimeout(120, TimeUnit.SECONDS)
.writeTimeout(120, TimeUnit.SECONDS)
.readTimeout(120, TimeUnit.SECONDS)
.retryOnConnectionFailure(true)
.build();
}





Ok thanks for reply .I try it
– Adam
Jun 29 at 10:33





Can you share with me your little bit code?
– Adam
Jun 29 at 10:43





Do you want full code for uploading file via multipart?
– Kamlesh
Jun 29 at 12:43






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV