I'm trying to use Unity to upload a .png file to a specific web folder. The issue could be 1 of 3 things.
1) I couldn't locate a php.ini file on godaddy. So could it be that the folder isn't configured for upload?
2) I've never done this in Unity before so it could be an issue with my code there. (it does return the message that my file couldn't be uploaded, so it is pinging the .php file)
3) It could also be the php code I have, not sure if I adapted the example properly.
I've read other examples but I'm just not sure where my issue even lies for what I should be fixing. Any advice would be awesome.
Unity:
public IEnumerator UploadTicketImage()
{
byte[] FileUpload = File.ReadAllBytes(Path.Combine(Application.persistentDataPath
+ "/TicketInformation/", "Pic001.png"));
WWWForm TextureForm = new WWWForm();
//TextureForm.AddBinaryData(PlayerPrefs.GetString("ImageName"), FileUpload);
TextureForm.AddBinaryData("TotalTest", FileUpload, Path.Combine(Application.persistentDataPath
+ "/TicketInformation/", "Pic001.png"),"image/png");
UnityWebRequest wwwimageupload = UnityWebRequest.Post(StaticVars.IPAddress+ "/UploadTicketsImage.php?", TextureForm);
wwwimageupload.uploadHandler = (UploadHandler)new UploadHandlerRaw(FileUpload);
//wwwimageupload.chunkedTransfer = false;
yield return wwwimageupload.SendWebRequest();
Debug.Log(wwwimageupload.downloadHandler.text);
}
PHP
<?php
$target_dir = "PhotoDeposit/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
?>
I would already expect it to not find the image file you want to upload correctly.
Path.Combine(Application.persistentDataPath + "/TicketInformation/", "Pic001.png")
results in a path like
| |
v v
C:\path\to\project\/TicketInformation/\Pic001.png
on windows or
| |
v v
/path/to/project//TicketInformation//Pic001.png
on linux or android.
It should rather be used like
Path.Combine(Application.persistentDataPath, "TicketInformation", "Pic001.png")
and
Path.Combine(Application.persistentDataPath, "TicketInformation")
The next point is: Why are you even using an UploadHandlerRaw when you already added the data to the WWWForm so this seems to make not much sense to me.
You should remove the line
wwwimageupload.uploadHandler = (UploadHandler)new UploadHandlerRaw(FileUpload);
Also note that File.ReadAllBytes only works on Windows platforms so depending on your build target you might have to change the way you read the file. You could use a UnityWebRequestTexture.GetTexture also to read local files. Something like
public IEnumerator UploadTicketImage()
{
var folderPath = Path.Combine(Application.persistentDataPath, "TicketInformation");
var filepath = Path.Combine(folderPath, "Pic001.png");
byte[] FileUpload = null;
using (var www = UnityWebRequestTexture.GetTexture(filepath))
{
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.LogErrorFormat(this, "File loading failed with {0} - {1}", www.responseCode, www.error);
Debug.LogErrorFormat(this, "Couldn't read file at {0}", filepath);
yield break;
}
else
{
// file data successfully loaded
FileUpload = www.downloadHandler.data;
}
}
var form = new WWWForm();
// here you want the name the file should have on the server
// not a complete system path to file on your local device
// |
// |
// |
// And this is the field you will have |
// to access in php |
// | |
// v v
form.AddBinaryData("TotalTest", FileUpload, "Pic001.png", "image/png");
using (var www = UnityWebRequest.Post("https://111.111.111.1" + "/UploadTicketsImage.php?", form))
{
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.LogError(www.error);
}
else
{
Debug.Log(www.downloadHandler.text);
}
}
}
Finally I am no PHP expert by afaik you will then have to access the submitted field like
$_FILES['TotalTest']
sicne you call the field TotalTest in form.AddBinaryData.
Also the
if(isset($_POST["submit"]))
is probably never true since you didn't set it in the WWWForm (I might be wrong and it is set automatically whenever a request is sent).
Note: Typed on smartphone so no warranty but I hope the idea gets clear.
Related
Trying to make Android chooser to display available actions for user to launch a PDF file which is stored in my local folder.
When I pass the file name like /data/user/0/myappappname/files/output.pdf , (which exsists, of course), I get a nice chooser with all the apps that can accept a pdf file. But when I pick any of them, I get an error (from external app) The document path is not valid. No exception is thrown.
Then I tried (for testing purposes) to set fname to something like /storage/emulated/0/Download/TLCL.pdf (file also exists), and everything works fine.
At first, I thought that this has something to do with file permissions (since first path is private to my app), but then I found flag ActivityFlags.GrantReadUriPermission built exactly for purpose of temporarily granting file access to other apps. Still same results.
Since this is a Xamarin.forms project, I am limited in choice of file creation locations (I use PCLStorage, which always writes to app-private, local folder), so I don't have an option of generating files in /Documents, /Downloads etc.
I am obviously doing something wrong. Any ideas appreciated.
Is there an option to get full path from system, including the /storage/emulated/0 part (or whatever that would be on other devices)? Maybe that would help?
Piece of code:
(mimeType is defined as "application/pdf" earlier)
public async Task<bool> LaunchFile(string fname, string mimeType)
{
var uri = Android.Net.Uri.Parse("file://" + fname );
var intent = new Intent(Intent.ActionView);
intent.SetDataAndType(uri, mimeType);
intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask | ActivityFlags.GrantReadUriPermission );
try
{
Forms.Context.StartActivity(Intent.CreateChooser(intent, "ChooseApp"));
return true;
}
catch (Exception ex)
{
Debug.WriteLine("LaunchFile: " + ex.Message);
return false;
}
My solution to this, which may not be exactly what you want, is to generate a file (in my case a zip file), export it to a public folder, and use that file for the chooser.
Using these:
private readonly string PublicDocsPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/AppName";
private readonly string PrivateDocsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
and some basic functions:
public Stream GetOutputStream(string destFilePath)
{
string destFolderPath = Path.GetDirectoryName(destFilePath);
if (!Directory.Exists(destFolderPath))
Directory.CreateDirectory(destFolderPath);
return new FileStream(destFilePath, FileMode.Create, FileAccess.Write, FileShare.None);
}
public Stream GetInputStream(string sourceFilePath)
{
if (!File.Exists(sourceFilePath)) throw new FileNotFoundException();
string sourceFolderPath = Path.GetDirectoryName(sourceFilePath);
return new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
}
You can copy your file to your public folder (or subfolders, you just have to assemble the path) and use that file for your chooser:
public void SendEmail(string subject, string body, string recipient, string mimeType, string attachmentFilePath, string activityTitle)
{
var emailIntent = new Intent(Intent.ActionSendMultiple);
if (string.IsNullOrEmpty(subject)) throw new ArgumentException();
emailIntent.PutExtra(Intent.ExtraSubject, subject);
if (!string.IsNullOrEmpty(recipient))
emailIntent.PutExtra(Intent.ExtraEmail, new[] { recipient });
if (!string.IsNullOrEmpty(body))
emailIntent.PutExtra(Intent.ExtraText, body);
if (!string.IsNullOrEmpty(attachmentFilePath))
{
var file = new Java.IO.File(attachmentFilePath);
file.SetReadable(true, true);
var uri = Android.Net.Uri.FromFile(file);
emailIntent.PutParcelableArrayListExtra(Intent.ExtraStream, new List<IParcelable>(){uri});
}
emailIntent.SetType(mimeType);
_activity.StartActivity(Intent.CreateChooser(emailIntent, activityTitle));
}
This chooser specifically lets the user send their file via email or google drive , but you can assemble it however you want. The attachmentFilePath of this function is the same as the string passed into the GetOutputStream function above.
we're using Acr.IO rather than PCLStorage and I recall that has a property that'll return the fullpath for you.
The code we're using is below, but I wonder if you're simply missing "file://" off the start of your path, as I noticed thats in our code, as well as this previous stackoverflow answer to a similar question as this one, open a PDF in Xamarin.Forms (Android)
We're using a dependency FileService on Android and using this code to open PDFs:
public void OpenNatively(string filePath) {
Android.Net.Uri uri;
if (filePath.StartsWithHTTP()) {
uri = Android.Net.Uri.Parse(filePath);
}
else {
uri = Android.Net.Uri.Parse("file:///" + filePath);
}
Intent intent = new Intent(Intent.ActionView);
var extension = filePath.Substring(filePath.LastIndexOf(".")+1);
if (extension == "ppt" || extension == "pptx") {
extension = "vnd.ms-powerpoint";
}
var docType = "application/" + extension;
intent.SetDataAndType(uri, docType);
intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
try {
Xamarin.Forms.Forms.Context.StartActivity(intent);
}
catch (Exception e) {
Toast.MakeText(Xamarin.Forms.Forms.Context, "No Application found to view " + extension.ToUpperInvariant() + " files.", ToastLength.Short).Show();
}
}
Iam building a website which uses a pivot viewer that uses a deep zoom collection to show images and the metadata from a database. Iam using the deezoomtools.dll from microsoft to create the deep zoom collection.
From this website i can start a thread that starts the creation of several collection. For small collections this process goes just fine, however when doing a large collection it fails regularly.
I added some debug lines to see what happens and the exception i get is:
Thread was aborted as the message and as stack trace where it came from.
I found that when the images are processed the exception occurs. I guess that the error occurs in this code sample in the function GetImageForDeepZoomComposer
private bool processImage(Item collectionItem, string backupImage,StreamWriter _logFile)
{
try
{
_logFile.WriteLine("start process");
if (string.IsNullOrEmpty(collectionItem.Image))
return false;
if (!File.Exists(collectionItem.ImagePath))
return false;
_logFile.WriteLine("File is there");
if (!AllImagesUnique) // check to see if we have processed this exact file before.
if (cacheOfImageIds.ContainsKey(collectionItem.ImagePath)) // we already have have processed this image so don't do it again
{
collectionItem.ImageId = cacheOfImageIds[collectionItem.ImagePath];
return true;
}
_logFile.WriteLine("Get Image: " + collectionItem.ImagePath);
string workingImage = GetImageForDeepZoomComposer(collectionItem.ImagePath, collectionItem.Id);
_logFile.WriteLine("Image aquired");
_logFile.WriteLine("collection name and id");
string deepZoomImage = DeepZoomImageDir + collectionItem.Id;
sendAction(string.Format("\tConverting image {0} to Deep Zoom Output file {1} ", workingImage,
deepZoomImage));
//imageCreator.Create(workingImage, deepZoomImage);
_logFile.WriteLine("set image surrogate");
SurrogateImageInfo sii = new SurrogateImageInfo(workingImage, "DeepZoomImages/" + Path.GetFileName(deepZoomImage) + ".xml");
_logFile.WriteLine("image surrogate set");
images.Add(sii);
if (!AllImagesUnique) // if we want to make sure we don't use the Deep Zoom Composer on the same file twice then add this image to the cache
cacheOfImageIds.Add(collectionItem.ImagePath, collectionItem.Id);
return true;
}
catch (Exception ex)
{
_logFile.WriteLine(ex.Message);
sendAction(
string.Format(
"\tSkipping current item cause of execption encountered while processing the image\r\n\t\t{0}",
ex.Message));
return false; // this item will not be added to the collection
}
}
internal string GetImageForDeepZoomComposer(string imageFile,int id)
{
if (imageFile.StartsWith("http://") || imageFile.StartsWith("https://"))
{
if (_client == null) // if we havent used the WebClient for this collection yet create one
_client = new WebClient();
string tempFile = ImageDownloadDir + Guid.NewGuid();
sendAction(string.Format("\tDownloading image '{0} to {1}", imageFile, tempFile));
_client.DownloadFile(imageFile, tempFile);
return tempFile;
}
else if (imageFile.StartsWith("\\") || imageFile.StartsWith("E:\\") || imageFile.StartsWith("e:\\"))
{
var dir = Path.Combine(DeepZoomImageDir, id.ToString() + "_files");
//if (!Directory.Exists(dir))
// Directory.CreateDirectory(dir);
var fixedSizeFileName = imageFile.Replace("_thumb", "_fs");
//create a fixed size image if it's not already created
if (!File.Exists(fixedSizeFileName))
FixedSize(new System.Drawing.Bitmap(imageFile), 512, 512).Save(fixedSizeFileName,codecInfo,parameters);
//copy the fixed size image to the DeepZoomImages folder so it can be used when zooming in to the deepest level
var localFixedSizeImage = Path.Combine(DeepZoomImageDir, id + ".jpg");
File.Copy(fixedSizeFileName, localFixedSizeImage);
return localFixedSizeImage;
}
return imageFile;
}
I think it has something to do with images that are locked when it tries to get the image and tries to save it with a different name such that it could be used. Iam truly at a loss here.
Thank you in advance for the information
Hi I have been making an app in Unity and I have been trying to add code that will log User actions i.e. they have made a certain game object appear. What I am trying to do is write to a .txt file stored on a server I have the following code which is based on:
http://answers.unity3d.com/questions/984290/upload-txt-file-to-server-using-php.html
and they suggest it works, for me it is not writing anything to the file.
Here is my PHP code (SaveData.php):
<?php
$Action = $_GET["action"];
$Date = date('Y/m/d H:i:s');
$myFile = "TestingData.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh,$Action . "," . $Date . "\n");
fclose($fh);
?>
And here is the C# (SaveData.cs) I am attaching to the game objects I change the Action value in the inspector window:
using UnityEngine;
using System.Collections;
public class SaveData : MonoBehaviour {
public string postDataURL = "http://arinstructionman.byethost7.com/SaveData.php?"; //add a ? to your url
public string Action;
void Start()
{
StartCoroutine(PostData(Action));
}
IEnumerator PostData(string action)
{
string post_url = postDataURL + "action=" + WWW.EscapeURL(action);
WWW data_post = new WWW(post_url);
yield return data_post;
if (data_post.error != null)
{
print("There was an error saving data: " + data_post.error);
}
}
}
If I copy the post_url value and run it in the browser it seems to work fine, can anyone advise what I am doing wrong?
post_url output:
http://arinstructionman.byethost7.com/SaveData.php?action=Plane+Visible
Any help is much appreciated.
I would suggest trying this with $_POST instead of $_GET and see if you get a different response.
Also, you can use file_put_contents to open, write, and close the file all in one line, and using the FILE_APPEND flag to prevent overwriting the existing file. If the file does not exist, this will also create the file for you.
<?php
if(!isset($_POST["action"]))
{
echo ("NO DATA RECIEVED");
return;
}
$action = $_POST["action"];
$date = date('Y/m/d H:i:s');
$data = "{$action},{$date}\n";
$myFile = "TestingData.txt";
if(file_put_contents($myFile, $data, FILE_APPEND))
echo ("success");
else
echo ("failed to write file);
?>
And use WWWform to POST data to the PHP file:
public string postDataURL = "http://arinstructionman.byethost7.com/SaveData.php";
public string Action = "some action";
void Start()
{
StartCoroutine(PostData(Action));
}
IEnumerator PostData(string action)
{
WWWForm form = new WWWForm();
form.AddField("action", action);
WWW dataPost = new WWW(postDataURL, form);
yield return dataPost;
if (dataPost.error != null)
print("There was an error saving data: " + data_post.error);
else
print(dataPost.text);
}
)
Just in case someone is trying to implement that and it doesn't work like it was the case for me.
I found a fix, for sure is not the best implementation, but at least it works for the moment :
If your server doesn't use PHP5 you will have to use PHP4 function
(coming from : http://memo-web.fr/categorie-php-126/ ):
function file_put_contents_PHP4($myFile, $data) {
$f = #fopen($myFile, 'w');
if (!$f) {
echo ("failed to write file");
return false;
} else {
$resultat= fwrite($f, $data);
fclose($f);
echo ("success");
return $resultat;
}
}
For sure we could test if PHP5 is available with something like, but my php knowledge is kind of bad ahah :
if (!function_exists('file_put_contents'))
{
function file_put_contents_PHP4...
{
The Scripts :
SaveData.php, in your server online:
<?php
$action = $_POST["action"];
$data = "{$action}";
$myFile = "TestingData.txt";
if($action == "")
{
echo ("NO DATA RECIEVED");
return;
}
else
file_put_contents_PHP4($myFile, $data);
function file_put_contents_PHP4($myFile, $data) {
$f = #fopen($myFile, 'w');
if (!$f) {
echo ("failed to write file");
return false;
} else {
$resultat= fwrite($f, $data);
fclose($f);
echo ("success");
return $resultat;
}
}
?>
Unity c# Script :
[SerializeField] private string postDataURL = "http://yourebsite.com/SaveData.php";
[SerializeField] private string Action = "some action";
void Start()
{
StartCoroutine(PostData(Action));
}
IEnumerator PostData(string action)
{
WWWForm form = new WWWForm();
form.AddField("action", action);
WWW dataPost = new WWW(postDataURL, form);
yield return dataPost;
if (dataPost.error != null)
print("There was an error saving data: " + dataPost.error);
else
print(dataPost.text);
}
Don't forget to put an empty text file "TestingData.txt" next to "SaveData.php" into the server.
Don't forget as well to add the correct adress to SaveData.php in the Inspector.
I uploaded them to an old personal server using Filezilla and everything is working as intended.
Cheers :-)
I previously made a post asking how to send a .3gpp audio file up to the parse cloud here:
Xamarin C# Android - converting .3gpp audio to bytes & sending to parseObject
I have managed to do this successfully, on parse's data manager, I can click the file's link and play the sound sent from my android device successfully.
Here's the code for uploading the data to the cloud:
async Task sendToCloud(string filename)
{
ParseClient.Initialize ("Censored Key", "Censored Key");
string LoadPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
string savetheFile = sName + ".3gpp";
string tempUserName;
LoadPath += savetheFile;
Console.WriteLine ("loadPath: " + LoadPath);
try
{
byte[] data = File.ReadAllBytes(LoadPath);
ParseFile file = new ParseFile(savetheFile, data);
await file.SaveAsync();
var auidoParseObject = new ParseObject("AudioWithData");
//Console.WriteLine(ParseUser.getUserName());
if (ParseUser.CurrentUser != null)
{
tempUserName = ParseUser.CurrentUser.Username.ToString();
}
else{
tempUserName = "Anonymous";
}
//tempUserName = ParseUser.CurrentUser.Username.ToString();
Console.WriteLine("PARSE USERNAME: " + tempUserName);
auidoParseObject["userName"] = tempUserName;
auidoParseObject["userName"] = tempUserName;
auidoParseObject["file"] = file;
await auidoParseObject.SaveAsync();
}
catch (Exception e)
{
Console.WriteLine("Failed to await audio object! {0}" + e);
}
}
So as you can see, I'm sending a ParseObject called "AudioWithData".
This object contains two children:
-The username of the user who uploaded the file (string)
-The parseFile called "file" (which has the following two children)
---SaveTheFile (A string containing the name of the audio file, input by the user, with the .3gpp extension added on the end, for example "myAudioFile.3gpp"
---data (this contains the bytes of the audio file)
I need to be able to download the file onto my android device, and play it through a mediaplayer object.
I've checked over the documentation on the parse website, but I haven't managed to do this:
(excuse my pseudo querying syntax here)
SELECT (audio files) FROM (the parseObject) WHERE (the username = current user)
I then, eventually, want to place all of these files into a listview, and when the user clicks the file, it plays the audio.
I've tried the following but I don't really know what I'm doing with it...
async Task RetrieveSound(string filename)
{
ParseClient.Initialize ("Censored key", "Censored key");
Console.WriteLine ("Hit RetrieveSound, filename = " + filename);
string username;
var auidoParseObject = new ParseObject("AudioWithData");
if (ParseUser.CurrentUser != null) {
username = ParseUser.CurrentUser.Username.ToString ();
} else {
username = "Anonymous";
}
string cloudFileName;
Console.WriteLine ("username set to: " + username);
var HoldThefile = auidoParseObject.Get<ParseFile>("audio");
//fgher
var query = from audioParseObject in ParseObject.GetQuery("userName")
where audioParseObject.Get<String>("userName") == username
select file;
IEnumerable<ParseFile> results = await query.FindAsync();
Console.WriteLine ("passed the query");
//wfojh
byte[] data = await new HttpClient().GetByteArrayAsync(results.Url);
Console.WriteLine ("putting in player...");
_player.SetDataSourceAsync (data);
_player.Prepare;
_player.Start ();
}
Any help would be GREATLY APPRECIATED! Even a point in the right direction would be great!
Thanks!
EDIT--
I'm actually getting a query error on the following lines
(I can't post images because of my reputation - I lost access to my main stackOverflow account :/ )
Links to images here:
first error: http://i.stack.imgur.com/PZBJr.png
second error: http://i.stack.imgur.com/UkHvX.png
Any ideas? The parse documentation is vague about this.
this line will return a collection of results
IEnumerable<ParseFile> results = await query.FindAsync();
you either need to iterate through them with foreach, or just pick the first one
// for testing, just pick the first one
if (results.Count > 0) {
var result = results[0];
byte[] data = await new HttpClient().GetByteArrayAsync(result.Url);
File.WriteAllBytes(some_path_to_a_temp_file, data);
// at this point, you can just initialize your player with the audio file path and play as normal
}
I'm hoping someone can modify my code below to show me exactly how to get this to do what I want.
I have an HTML form that posts to the following action:
public ActionResult Create(string EntryTitle, string EntryVideo, HttpPostedFileBase ImageFile, string EntryDesc)
{
if (Session["User"] != null)
{
User currentUser = (User)Session["User"];
string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(ImageFile.FileName));
ImageFile.SaveAs(savedFileName);
Entry newEntry = new Entry();
newEntry.Title = EntryTitle;
newEntry.EmbedURL = EntryVideo;
newEntry.Description = EntryDesc;
newEntry.ImagePath = savedFileName;
newEntry.UserId = currentUser.UserId;
db.Entries.Add(newEntry);
db.SaveChanges();
}
return RedirectToAction("MyPage", "User");
}
This saves the image to the root solution directory (or tries to, doesn't have permission and throws exception instead).
What I would like it to do is the following:
1.) Verify that the file size is under some maximum, let's say 500kb for now
2.) Assuming file size is okay, save it to the following directory
mywebsite.com/uploads/<userid>/<entryid>/entry-image.<jpg/png/gif>
I'm not sure how to rename the file since I want to accept different file extensions (.jpeg, .jpg, .png, .gif). Or unsure how to get it into the correct directory like above. Or how to validate file size, since apparently you can only do that with javascript if the user is using IE.
1.Verify that the file size is under some maximum, let's say 500kb for now
You can use the HttpPostFileBase.ContentLength property to get the size (in bytes) of the file.
if (ImageFile.ContentLength > 1024 * 500) // 1024 bytes * 500 == 500kb
{
// The file is too big.
}
2.Assuming file size is okay, save it to the following directory
string savedFileName = Server.MapPath(string.Format("~/uploads/{0}/{1}/entry-image{2}",
currentUser.UserId,
newEntry.EntryId,
Path.GetExtension(ImageFile.FileName)));
The only problem I see is that it looks like your Entry.EntryId might be generated at the database so you won't be able to use it as part of the save path until it's been generated.
hope this helps or at least points you in the right direction
if (ImageFile.ContentLength < 1024 * 500)
{
Entry newEntry = new Entry();
newEntry.Title = EntryTitle;
newEntry.EmbedURL = EntryVideo;
newEntry.Description = EntryDesc;
newEntry.UserId = currentUser.UserId;
db.Entries.Add(newEntry);
db.SaveChanges(); //this helps generate an entry id
string uploadsDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "uploads");
string userDir = Path.Combine(uploadsDir, <userid>);
string entryDir = Path.Combine(userDir, newEntry.EntryID );
if (Directory.Exists(userDir) == false)
Directory.CreateDirectory(userDir);
if (Directory.Exists(entryDir) == false)
Directory.CreateDirectory(entryDir);
string savedFileName = Path.Combine(entryDir, <entry-image>);
ImageFile.SaveAs(savedFileName);
newEntry.ImagePath = savedFileName; //if this doesn't work pull back out this entry and adjust the ImagePath
db.SaveChanges();
}
You should grant a write permission to the 'uploads' directory.
you can also limit file sizes for your web app from the web.config
<system.web>
<httpRuntime maxRequestLength="500"/>