Purpose: Upload images with parameters
Environment: Client Development Android Studio, Server side: Visual Studio (C # ASP)
************************************************************************
When uploading a picture, I encountered a return filenotfoundexception, but always error on the client.
HttpURLConnection in Connection.getinputstream () Report Error FileNotFoundException
Java.io.FileNotFoundException: http://192.168.11.6/appHandler/UploadHandler.ashx (always thought to be unable to find this file)
So also has been looking for the client's error, in exchange for, or did not solve, are going crazy, put a period of time, today again to supplement this piece of development, slowly debugging, only to find that if the server side only accept parameters, do not process the picture and file this piece, will return to the server message normally, This is to know that there is a problem on the server side, the following code to post the problem:
Public stringUploadvoicefile (HttpContext context) {stringFileName, FilePath; Httppostedfile Itemimagefile= Context. request.files["Filedata"]; FileName= ItemImageFile.FileName.Substring (ItemImageFile.FileName.LastIndexOf (".")); //FileName = Guid.NewGuid (). ToString (). Substring + DateTime.Now.ToShortDateString (). Replace ("/", "_") + file. Filename.substring (file. Filename.lastindexof (".")); stringFilepathstr ="/upload/voice/"+ DateTime.Now.ToShortDateString (). Replace ("/","") +"/"; FilePath=HttpContext.Current.Server.MapPath (@FilePathStr); if(Itemimagefile! =NULL) { //If the directory is not available, create the upload directory if(! Directory.Exists (FilePath))//There 's a problem with this piece.{directory.createdirectory (FilePath); } if(Itemimagefile.contentlength >2097152)//image size cannot exceed 2M { return "{\ "result\": \ " -1\", \ "msg\": \ "picture size cannot exceed 2m! \"}"; } itemimagefile.saveas (FilePath+FileName); return "{\ "result\": \ "1\", \ "msg\": \ ""+ filepathstr + FileName +"\"}"; } return "{\ "result\": \ " -1\", \ "msg\": \ "file data is empty! \"}"; }
is not to create a directory, and can not save the file, so thrown FileNotFoundException, also passed back to the client, thought to be unable to find the file:http://192.168.11.6/appHandler/UploadHandler.ashx
There are friends on the Internet, said to re-create a project on the OK, I here through the server local debugging is also possible, so the reason appears in the IIS file permissions this piece
Find the file directory to upload, right-click Security-"add-all Users, permissions: Full Control, Done!"
Shame ah, this a small problem wasted a few days of development time, written here for later notes, but also for your reference!
Android Development upload picture encountered return FileNotFoundException